Search code examples
iosxcodeswiftsegueunwind-segue

Unwind Segue - Where to Drag Exit?


I am trying to understand how the unwind segue works. First I created 3 ViewControllers.

GlobalVC
 -LoginVC
 -RegisterVC 

I set the initial page the GlobalVC (only authenticated user can see). I set a segue from:

GlobalVC to LoginVC called globalToLogin and LoginVC to RegisterVC called loginToRegister. Here is my storyboard:

enter image description here

For the registerToLogin direction, I used this work-around (in RegisterVC), but looks a bit ugly.

@IBAction func unwindToLogin(sender: AnyObject) {
    //Dismiss View
    dismissViewControllerAnimated(true, completion: nil)
}

However, now I am trying to direct from LoginToGlobal

First, I selected Login button, dragged it to LoginVC:

@IBAction func loginButton(segue: UIStoryboardSegue) {
    performSegueWithIdentifier("unwindToGlobal", sender: self)
}

After setting segue: UIStoryboardSegue, now it gets available to right-click on Exit. But, I got confused here. Whatever I tried to drag the circle inside Exit to, it gives an error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', >reason: 'Receiver (0x7fdd2068a480>) has no segue with identifier 'unwindToGlobal''

Where should I drag the circle on? ( I keep seeing Unwind segue to 'Exit' )

enter image description here


Solution

  • The unwind action method is defined in the destination view controller (e.g. in GlobalVC).

    @IBAction func unwindToGlobal(segue: UIStoryboardSegue) {
         // this may be blank
    }
    

    And then, when you're hooking up the button to this action, you don't drag to the code sample, but rather to the "exit outlet" icon above the scene:

    enter image description here

    See this answer for more screen snapshots.