My target include a lot view need to present different view modally base on each user action. Here what I want to do to get cleaner view hierarchy and better user experience
What you want is an "unwind segue":
https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8
https://developer.apple.com/library/archive/technotes/tn2298/_index.html
It allows you to dismiss multiple view controllers at the same time, without having to know how many there are in the stack.
In VC1 you would implement an IBAction
called (for instance) unwindToRoot
. Then in the storyboard for VC3, you wire up your Done button to the Exit
object and choose the unwindToRoot
action.
When that button is pressed, the system will dismiss all the view controllers it needs to bring you back to VC1.
This is better than calling presentingViewController?.presentingViewController?.dismiss()
, because VC3 doesn't need to know anything about the view controller hierarchy underneath it.