Unwind Segue in Xcode 6 Beta 4

2019-02-21 18:23发布

问题:

I've been trying to add an unwind segue to a swift app in Xcode 6, and, in the release notes for the first three betas, it was declared to be unsupported. However, in Beta 4, that bug note is no longer present. I have heard that people have been able to get it to work, but I have had no such luck. So, my question is this: how should I try to see if the unwind segue will work in my app? What should I try? I am using a UIBarButtonItem to trigger the segue.

So far, I've put this code in the destination controller's .swift file:

@IBAction func unwindToSegue(segue:UIStoryboardSegue) {}

Using this code, I was able to connect the bar button to the function by control-dragging from the bar button to the exit icon, and then clicking the unwindToSegue method. That part has worked, but the code doesn't run, even when I put a breakpoint inside the function it wasn't called.

回答1:

You need to put your unwindToSegue function in the originating ViewController (that is, the ViewController you are trying to return to).

Another thing you can try is to call the segue programmatically. First, wire an exit segue from the view controller icon to the exit icon (the three icons are all on the top of the view controller in Interface Builder). Give that segue a name such as "goBackHome". Then wire your button to call this @IBAction:

@IBAction func headHome() {
    println("Button seen.  Trigging exit segue manually...")
    self.performSegueWithIdentifier("goBackHome", sender: self)
}


回答2:

My problem was that I segue'd to a standard view controller(VC) from a custom container VC, but trying to segue back lost the original nav bar. Researching forums revealed that unwinding the segues would "pop" the new VC "protocols" off the stack & allow me to return to the original VC "protocols" unchanged.

I dragged in a button to the new VC, first dragged it to the swift file to create the action "back2main", modified it as below, THEN dragged the button to the exit icon at the top of the new VC itself, which generates the "unwind segue".

@IBAction func back2main(segue: UIStoryboardSegue) {
    dismissViewControllerAnimated(true, completion: nil)
}


回答3:

With XCode 6 Beta 4 it works. Remove the @objc(classname) from the class definition and delete the (workaround) .h file.

I had a similar effect yesterday, one of my buttons in my Viewcontroller didn't trigger the unwind segue.

Try that: Drop a new UIButton on your view controller and leave it as it is. Don't change anything just to make sure that there are no side-effects. Then connect this button to your unwind segue and try if it works.



回答4:

I had the same issue. the unwind function was showing when I selected the exit button and it was connecting and all, but when taping the button, nothing happened.

After some messing around I realized that it was due to the Segue I had selected to transit from one View Controller to the other.

The problem turns out was that the view I wanted it to return to had more than a few different navigation components, which upon displaying the "child" view got removed. I solved it by changing it from Show (or show detail) to popover.

Go to the segue, select the Attribute Inspector and from there you can change the segue.