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?

Karthik Kumar
  • 1,165
  • 1
  • 9
  • 29
IlyaGutnikov
  • 557
  • 1
  • 6
  • 19
  • 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) – pbodsk Oct 07 '16 at 11:18
  • @pbodsk I try both: `_segue` and `_ segue` and it doesn't work. My educational project with same problem: https://github.com/IlyaGutnikov/SwiftStoryBoardsTutorial – IlyaGutnikov Oct 07 '16 at 11:27
  • 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: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/) – pbodsk Oct 07 '16 at 11:30
  • @pbodsk Yes, you are right. I had to add segue code to my `ViewController`. Thank you! – IlyaGutnikov Oct 07 '16 at 11:39
  • You're welcome, I'll take the liberty of adding an answer with the result, OK? – pbodsk Oct 07 '16 at 11:41
  • @pbodsk Of course. – IlyaGutnikov Oct 07 '16 at 11:44

1 Answers1

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.

pbodsk
  • 6,371
  • 3
  • 20
  • 47
  • 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. – Chuck H Aug 22 '17 at 18:02
  • 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 ¯\_(ツ)_/¯ – pbodsk Aug 22 '17 at 19:01