简体   繁体   中英

Why is interface builder not showing my unwind segue?

My app's navigation flow looks a bit like this:

UINavigationController - MasterViewController > DetailViewController > InfoViewController

MasterViewController contains the following method:

@IBAction func unwindToMaster(with segue: UIStoryboardSegue) {}

In DetailViewController, there is a similar method:

@IBAction func unwindToDetail(with segue: UIStoryboardSegue) {}

I use these methods, along with UIButtons, to allow the user to advance forward and back through the navigation hierarchy. In interface builder, I see the method under DetailViewController when I right click on "Exit" in InfoViewController, but when I right click on "Exit" in DetailViewController, no other unwind segues are listed.

I have consulted multiple online sources (Ray Wenderlich, relevant StackOverflow questions) instructing the correct way to produce unwind segues in interface builder, but none of these have helped solve the issue. Right now, I need help figuring out what the problem is in the first place. As development usually goes, it's probably something staring me square in the face.

DetailViewController中的退出菜单 - 'unwindToPersonWith:'='unwindToDetail'

I am running Xcode 8.1 using Swift 3. Thank you.

To re-iterate: not only an Action for a Storyboard Unwind Segue has to be placed in the same source file as class definition for an unwind-to (destination) View Controller (eg, @IBAction func prepareForUnwind(segue: UIStoryboardSegue) , detected as prepareForUnwind[With]Segue in a "Presenting Segues" list), but also that View Controller cannot have ANY extensions in ANY secondary source files . You have to merge class definition and all extensions into a single source file.

(As of Xcode 8.2.1.)

In my case, I had a complicated inheritance in my view controller and it was a reason why Interface Builder did not see my unwind. So this workaround works for me well:

  1. Go to YouCollViewController.swift file and delete all classes, protocols, generics, etc. your view controller implements in view controller's declaration
  2. Inherit your view controller from UIViewController
  3. Go to the storyboard and connect your action with the unwind , it should appear in unwinds list
  4. Go back to YouCollViewController.swift file and discard all the changes

I consulted Apple's Developer Library (specifically the page "Using Unwind Segues" ). There, the example definition of an unwind action is:

@IBAction func unwindToMainMenu(sender: UIStoryboardSegue) {
    let sourceViewController = sender.sourceViewController
    // Pull any data from the view controller which initiated the unwind segue.
}

Applying this example in my code, I changed my unwind action declarations to:

@IBAction func unwindToMaster(sender: UIStoryboardSegue) {
    print("Unwinded to master.")
}

and

@IBAction func unwindToDetail(sender: UIStoryboardSegue) {
    print("Unwinded to detail.")
}

I also made sure that each method was contained within the same file as MasterViewController's class declaration. After further testing, I found that all extensions of MasterViewController had to exist in the same file for interface builder to find and recognize the unwind segue.

Now, in storyboard, the exit menu shows both unwind segues. My conclusion is that by fiddling around with where the methods are placed and how they are declared, a configuration that interface builder can recognize will be found. I hope it will be less touchy in the future, as my current method organization is very long and difficult to navigate.

在此输入图像描述

Because none of these answers helped me and I didn't want to mess around with my code that much. Here another solution you can give a try.

Starting from the point where you have already added the unwind function to your MasterViewController.

  1. First I have gone ahead and added a new ViewController to my storyboard as well as a new Cocoa Touch Class File of type UIViewController and connected it with my Storyboard one (I called it the HelperViewController).

  2. Then you can add the same unwind function, you already have inside your MasterVC to your newly created HelperVC.

    @IBAction func unwindToMaster(with segue: UIStoryboardSegue) {}

  3. Now connect the Helper to your DetailVC. Your Storyboard should look somewhat like mine. You should get the same option as me if you ctrl + drag and drop to the exit of your DetailVC. When the connection has been established and you hover over the newly created segue both the Master and the HelperVC should be highlighted. Now you can delete the HelperVC again and everything should work as expected.

I hope that helped!

我的故事板

With swift 4 XCode 9.2 I found that you need to drag the reference from the yellow icon at the top of the viewController to the "Exit" reference of the target viewController in the left details panel. Hope this helps!

有时XCode重启会使操作出现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM