Search code examples
iosuiviewcontrollersegueuistoryboardunwind-segue

Is it possible to use an unwind segue from a single modal view controller back to one of multiple instances of the same source view controller?


My Storyboard layout is such that I have MainVC connected to ModalVC via a segue that presents it modally.

However, programmatically, at runtime, I instantiate five instances of MainVC in total.

As a result, all five instances of MainVC, with the same identifier, are connected to ModalVC.

I need to change properties in MainVC after performing some actions in ModalVC and dismissing it, and there appear to be various ways to do this via segues.

The top answer here is quite detailed and explains the implementation of using an unwind segue but only in the scenario of there being one source view controller: Passing data with unwind segue

My question is: will using a segue, like in that answer, or any other way, work in my situation, with multiple instances of the same MainVC (same identifier) attached to a single instance of ModalVC?

Will the unwind segue only transfer data or perform actions in the single MainVC instance that initiated it? Or will it end up affecting all five MainVC instances because the MainVC instances have the same identifiers? If the latter, is there any way around this?


Solution

  • Apple has a comprehensive technical note on how unwind segues work and how the destination view controller is determined, but, in summary, the process examines the view controller navigation hierarchy to find the first view controller that can handle the unwind segue and is willing to do so.

    In your case, this would be the MainVC instance that presented the ModalVC that is unwinding. The unwind segue cannot be handled by a view controller instance that is not in the navigation hierarchy (e.g. An instance of MainVC that did not present the ModalVC)