10

I have been reading that unwind segue's are bugged in Xcode 6. I am using Xcode 6.1.1 and I use swift.

I use the "Back" button that is put by the navigation controller. Thus I can't cntrl drag to the exit icon. Moreover I can't drag from viewController icon to exit icon either.

Is this a bug? Or else I am missing some fundamental knowledge about how to use unwind segue. How can I set unwind segues from the storyboard?

Berkan Ercan
  • 1,207
  • 3
  • 14
  • 31

4 Answers4

9

I want to provide a detailed answer:

To perform an unwind segue, go to the view controller that you want to segue to and add the following function; (the name can change of course)

@IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {

}

Once you add this function, you will be able to see this unwind function from any view controller on your storyboard. Just right click on the exit icon on the top of any view controller.exit icon right click

If you want to perform unwind with a button, then you can cntrl + drag from the button to exit icon and choose your unwindSegue function. Done!

Unwind with Button

If you want to perform unwind programmatically, then cntrl + drag from the viewController icon to the exit icon and choose the unwind function.

enter image description here

Afterwards, open the Document Outline and click on the unwind segue.

enter image description here

Go to Attributes Inspector inside Utilities and give an identifier to your unwind segue.

enter image description here

Lastly, call the performSegueWithIdentifier function like the following;

self.performSegueWithIdentifier("goToMainMenu", sender: self)

Hope that helps!

Berkan Ercan
  • 1,207
  • 3
  • 14
  • 31
3

Got it working. If you are presenting from a NavigationController or TabBarController(unsure about the TabBarController), you need to subclass the navigation controller and add the unwind segue to that. In storyboard don't forget to change the class of your navigation controller. My view hierarchy was NavController -> TableController -> DetailController. After adding method to custom NavBar class add it to the VC you want to RETURN to. You will then be able to ctrl-drag to exit. I consider this a bug, as once it is hooked up I was able to delete the subclass and return to stock NavigationController and it still worked.

hidden-username
  • 2,610
  • 3
  • 14
  • 19
  • 5
    I tried it without custom navigationController class and still worked. Apparently, adding `@IBAction func unwindSegue(segue: UIStoryboardSegue) { }` on the destination view controller enables the dragging to exit icon – Berkan Ercan Dec 07 '14 at 20:41
  • Can you please try that as well? If it works it would be better to write that as an answer. – Berkan Ercan Dec 07 '14 at 20:43
  • My bad, I thought you were already doing that. You Always have to declare the unwind in the destination VC, not the current VC, in order to ctrl-drag to exit. But for me, I had to subclass my navigation controller in order to drag in IB. And declare it in the destination VC and custom NavVc. – hidden-username Dec 07 '14 at 20:48
  • Yes, so far I have always used the navigationBar buttons. I am gonna use unwinding to actually pass data. Thanks a lot for your efforts! – Berkan Ercan Dec 07 '14 at 20:50
  • No problem. Just a heads up. If you try passing objects you may have to use a temporary container. Ie if you want to fill a label with text from the origin VC, you cannot put it directly into the destination VC label. You have to declare a string property along with a label property in the destination VC and store it in there. Than instantiate the destination label with the destination string in viewDidLoad. – hidden-username Dec 07 '14 at 20:59
1

Got it working on Xcode 6.2. My case is ViewController A presenting B modally. Unwind setup steps below:

  1. In A, write an IBAction

    -(IBAction)unwindFromB:(UIStoryboardSegue *) unwindSegue 
    {
      ViewControllerB *vc = [unwindSegue sourceViewController];
      // get whatever data you want to pass back from B
    }
    
  2. In storyboard, find B and right-click the 'Exit' button at the top, then the 'Presenting Segue' menu will list unwindFromB you've just created in A

  3. Click the little '+' sign to the right and link it back to B, select 'manual'

  4. Don't forget to set identifier of the unwind segue if you need to call it programmatically.

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
Kitetaka
  • 527
  • 4
  • 20
0

I found that I could not drag from the UITableViewCell to the Exit object on the View Controller on the Storyboard.

Solution was to first add an appropriate IBAction in code. Once an IBAction exists, the drag target will light up.

-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {

}
pkamb
  • 33,281
  • 23
  • 160
  • 191