2

Consider our Main.storyboard

SceneAViewController ---> SceneBViewController ---> SceneCViewController

SceneAViewController has a method @IBAction func after_unwind(segue: UIStoryboardSegue)

SceneBViewController has a method @IBAction func after_unwind(segue: UIStoryboardSegue)

How does Xcode determine which Scene/ViewController to unwind to?

enter image description here

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99

1 Answers1

2

Unwind segues involve a run-time process that navigates from the current view controller, back up the chain of preceding view controllers, looking for the first instance it encounters that has an unwind action of the appropriate name. This means that it unwinds to the most recent occurrence.

So, if you go from view controller A to B to C, and A and B have an unwind segue of the same name, when you unwind from C using the unwind segue of that name, it will unwind to B, the most recent one, not A.

The run-time process of walking up the chain of view controllers is outlined in WWDC 2012 video Adopting Storyboards in Your App.


The above begs the question of the best choice for unwind action names. It's easy to jump to the conclusion of "just use unique unwind action names", but I think it's more complicated than that. Whether you use unique unwind action names or common/shared action names is just a question of the intent of the unwind action.

For example, sometimes you want to unwind all the way back to a particular scene. In that case you'd use some unique unwind action name. So, in our A » B » C example, A might implement unwindToA and B might implement unwindToB.

But consider some flow by which the user could possibly navigate from A » B » D or alternatively A » C » D and you want D to unwind to either B or C, as appropriate. In that case, B and C might implement a unwindFromD action.

I suspect it was the desire for this sort of flexibility that prompted Apple to implement this sort of "walk up the chain of view controllers" run-time process rather than a "look for view controller of particular class" sort of approach.

Regardless, the choice of unique vs shared unwind action names simply depends your particular use-case.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Does this mean there is no way to specify a specific `Class` along with the normal method `Signature`? – AlanSTACK Feb 24 '18 at 06:10
  • Note that B can choose not to be the unwind target by implementing `canPerformUnwindSegueAction:fromViewController:withSender:` to return `NO` (in Swift: `canPerformUnwindSegueAction(_:from:withSender:)` / `false`). – rob mayoff Feb 24 '18 at 06:11
  • @AlanSTACK - Specifying the class is somewhat redundant if each view controller class you can unwind to has a unique unwind action name. And it's not the standard "call such-and-such method in such-and-such target or class", but a runtime process that it walks back up the chain of view controllers looking for one that has implemented a method of the required name, and uses that to figure out what to unwind to (Rob Mayoff's caveat notwithstanding). – Rob Feb 24 '18 at 06:15
  • Thank you for your response. – AlanSTACK Feb 24 '18 at 06:54