2

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

senty
  • 12,385
  • 28
  • 130
  • 260
  • Already fully explained here. http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them. I am of a mind to mark it as duplicate but I want you to confirm first that what is not covered in that answer which provides valid grounds for you to post a new question – NSNoob Dec 01 '15 at 05:16
  • Simply, the unwind segue has to be drawn at the last VC, the one from which you are trying to exit. The unwind method is written in the destination VC e.g. the first Vc. – NSNoob Dec 01 '15 at 05:19
  • My problem is that I am seeing "Unwind segue to 'Exit'", I think I am doing something wrong but I couldn't figure out what. When I right-click on Exit, I see segues but whatever I drag it to, it doesn't change showing "Unwind segue to 'Exit'" – senty Dec 01 '15 at 05:22
  • See [Technical Note #2298](https://developer.apple.com/library/ios/technotes/tn2298). By the way, unlike described in the link that NSNoob shared with us, the bar with the exit outlets is now above the view controller's scene in IB, not below it as shown in that other answer, which was for an older version of Xcode. – Rob Dec 01 '15 at 05:25
  • Your error however suggests otherwise. That your LoginVc has no such segue named unwindToGlobal. – NSNoob Dec 01 '15 at 05:26
  • @Rob If you ask me, It is just a trivial detail. The method remains the same which is more important. But well that's just me. you have a valid point however and that is a useful link – NSNoob Dec 01 '15 at 05:28
  • I got confused somewhere half-way. From the beginning: I ctrl-dragged Login button to VC. Then I changed `sender: AnyObject` to `segue: UIStoryboardSegue`. It made the right-click exit available. And whatever I drag it to, I keep receiving `Unwind segue to 'Exit'`. What am I doing wrong? – senty Dec 01 '15 at 05:29
  • @senty No, I think you're conflating an `@IBAction` for performing some code when you click on a button (in which the parameter might be `sender: AnyObject`), with the action method (`@IBAction func unwindToGlobal(segue: UIStoryboardSegue) { ... }`) which is defined in the parent view controller and for which you do _not_ control-drag to the function, but rather you drag to to the "exit outlets" in the bar above the scene in IB. But do not drag from a button directly to the unwind action function code. – Rob Dec 01 '15 at 05:32
  • @Rob I am still very confused. I rolled everything back now. Am I doing wrong in the first step, dragging Login button to view controller and changing sender: AnyObject to segue: UIStoryboardSegue? – senty Dec 01 '15 at 05:38
  • No. If you are doing an unwind segue, you do not have _any_ method in the view controller in scene with the button. And if that button was previously hooked up to an `@IBAction` method, I'd remove that connection first. The unwind action `@IBAction` (with the `UIStoryboardSegue` parameter) is in the view controller that you're dismissing back to. And you're not hooking the button directly to the code, but rather to that "exit outlets" icon above the scene. – Rob Dec 01 '15 at 05:42

1 Answers1

5

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.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Ah, so I need to put the unwind function in the parentVC. But, where is the @IBAction linked in the storyboard? – senty Dec 01 '15 at 05:42
  • 1
    You don't hook that unwind action `@IBAction` code at all. The storyboard does that for you. When you control-drag to that exit icon, IB looks at the code for the parent view controller(s) and finds an `@IBAction` with `UIStoryboardSegue` parameter, and then presents that as the options for you. You pick that action from a little dropdown, but you do not hook the button to the unwind action `@IBAction` code directly yourself. IB does that for you. – Rob Dec 01 '15 at 05:44
  • Ohh!! I made it work!! Thanks man! Was a bit confusing not assigning IBAction to anything. Cheers – senty Dec 01 '15 at 05:47