Unwind segue doesn't work in Swift 3 and iOS 10
Asked Answered
D

1

10

I'm trying to test unwind segue in iOS 10 and Swift 3.

I made a simple app like this: enter image description here

enter image description here

I add code for segue in TableViewController class and connect "Cancel" button and Exit on Table View Controller Scene:

@IBAction func unwindToRootViewController(segue: UIStoryboardSegue) {
    print("Unwind to Root View Controller")
}

but my simple segue doesn't work. What am I doing wrong?

Dying answered 7/10, 2016 at 11:14 Comment(6)
what happens if you add a _ before segue like so: @IBAction func unwindToRootViewController(_ segue: UIStoryboardSegue) (and then you might have to reconnect your segue in Interface Builder)Crabbing
@Crabbing I try both: _segue and _ segue and it doesn't work. My educational project with same problem: github.com/IlyaGutnikov/SwiftStoryBoardsTutorialDying
Oh...I just noticed that you said that you had added code for the segue in your TableViewController, shouldn't it be in the "outer" ViewController? (have a look here: spin.atomicobject.com/2014/10/25/ios-unwind-segues)Crabbing
@Crabbing Yes, you are right. I had to add segue code to my ViewController. Thank you!Dying
You're welcome, I'll take the liberty of adding an answer with the result, OK?Crabbing
@Crabbing Of course.Dying
C
10

As can be seen in the comments above the problem was this:

I add code for segue in TableViewController class

The unwind segue has to be in your "outer" ViewController, not the TableViewController.

This article gives a very nice introduction to unwind segues.

Crabbing answered 7/10, 2016 at 11:46 Comment(2)
I was porting some working objc code to swift and none of my unwind segue's worked. Your first comment in the question above was the solution to my problem as I was missing the "_" in my method signatures. Sometimes single character errors are the most difficult to find. Thanks.Elmore
You're most welcome and yes sometimes you spend hours on a problem and the solution ends out being a single character added in juuuust the right place...programming eh ¯_(ツ)_/¯Crabbing

© 2022 - 2024 — McMap. All rights reserved.