I started learning iOS/Swift recently and wondering how to use unwind segue.
Assume the following view controller A, B, C, E, F, X, BB, BBB
C -> A -> E
C -> B -> BB -> E
C -> B -> BBB -> E
C -> D -> E
F -> X -> E
How do I use unwind segue to go back to C or B or D from E?
Note: In ViewController E, there is only one Back
button.
I have tried the following solution:
// Add this method in C, B, and D view controller
@IBAction func unwindFromE(sender: UIStoryboardSegue) {}
This seems to work some how, but why did it unwind back to C for F -> X -> E even though in view controller F/X there's no unwindFromE method.
As you have discovered, you can implement the unwind target @IBAction
in multiple viewControllers and iOS will go up the call chain until it finds one. This allows viewController E
to return to whomever called it as long as they implement the target function unwindFromE
.
In the case of your E
unwind returning to C
when the call stack was F->X->E
, this happens when C
and F
are viewControllers controlled by the same UITabBarController
. Since F
doesn't implement unwindFromE
, iOS searches the other viewControllers controlled by the UITabBarController
. In your case, it found C
and switched to that tab.
More information about unwind segues can be found in Technical Note TN2298: Using Unwind Segues.