1
votes

I've followed the instructions here and here which suggest that I should be able to create an unwind/edit segue even when using a storyboard reference.

The flow is quite simple:

PhotoDetailScene has a storyboard reference to EditHistoryScene and a tap gesture to trigger the segue, then I have a close button on EditHistoryScene that unwinds back to PhotoDetailScene. The present works fine (the custom 'show' segue opens the EditHistoryScene), and if I use a dismiss() call it dismisses fine. But unwinding with the segue identifier always fails with:

EditHistoryScene has no segue with identifier 'UnwindEditHistorySegue'

In my EditHistoryScene close button action, I have:

func closeButtonPressed(_ sender: Any) 
    performSegue(withIdentifier: "UnwindEditHistorySegue", sender: self)
}

And in my PhotoDetailScene, I have added:

@IBAction func unwindToPhotoDetailScene(_ unwindSegue: UIStoryboardSegue) {
    print("It works!")
}

And here's how I've set it up in IB, with the unwindToPhotoDetailScene() method connected to the storyboard reference (since that's the only way IB would let me ctrl+link the outlet):

Presenting segue: enter image description here

Unwind segue properties (and segue name) enter image description here

Storyboard reference correctly set for destination storyboard: enter image description here

Storyboard reference's exit point connected back to PhotoDetailScene: enter image description here

EditHistoryScene storyboard instance appears to not be connected to the @IBOutlet? enter image description here

My best guess is that it's failing because this is a storyboard reference, and the unwind segue is defined on the reference, not the actual storyboard instance. But I can't figure out how to attach the EditHistoryScene's exit point to the PhotoDetailScene directly.

1
I tried this; I just added a storyboard reference to the first storyboard to the second storyboard. As soon as I did that I was able to ctrl-drag from the button to the "Exit" icon in that button's view controller and select my unwind function from the pop-up. No need for any code and no need to name any segues.Paulw11
Ah! Nicely done. Want to expand on that and add it as an answer? I'll accept.brandonscript

1 Answers

3
votes

You don't need to create any segues to the reference itself.

Once your second storyboard contains a reference to the first storyboard, any unwind functions defined in the first storyboard are available.

You create exit segues by dragging from the exit trigger to the exit icon in the view controller scene.

If you want to trigger the unwind from your "close" button, simply ctrl-drag from the close button to the "exit" icon at the top of its view controller and you will see unwindToPhotoDetailScene as an option.

You only need to give the segue an identifier if you want to trigger it programatically.

For example, you might want an action handler function on the close button that checks to see if there are unsaved changes. If there are, prompt the user to confirm that they will lose their changes (or to save or whatever). Once they are happy to proceed you can the invoke the unwind.

To set this up, ctrl-drag from the view controller object to its own exit icon. Create the unwind segue and give it an identifier.