1

I have a view controller (in a navigation controller) A that segues to View Controller B, and View Controller C also segues to B (A -> B <- C). View Controller B is used to select a value that is passed back to either A or C, but if I click 'Cancel' or 'Done' it uses the unwind segue to go back to only one controller, whichever was connected last.

Lorenzo
  • 3,293
  • 4
  • 29
  • 56
Aaron
  • 91
  • 1
  • 3

1 Answers1

8

You can have an unwind segue return to whichever viewController initiated the segue. All you have to do is to implement the same method you are returning to in all of the viewControllers which segue to viewController B.

So in viewController A and viewController C, implement the following method:

@IBAction func backFromB(segue: UIStoryboardSegue) {
    print("Back from B")
}

Then, when you Control-drag from your Cancel button in viewController B to the Exit icon at the top of the viewController, select backFromB from the pop-up.

drag to set up exit segue


choose method to return to


Then when you run the app and hit Cancel in viewController B, you will return to either viewController A or viewController C (whichever one segued to B). This even works if one segue is a Show (Push) and the other is Modal.

demonstration picture

vacawama
  • 150,663
  • 30
  • 266
  • 294