简体   繁体   中英

Dismissing parent view controller of double modally presented view controller

I have the current setup where -> is a modal presentation ( presentViewController() ):

ViewController A -> NavigationController B:
    ViewController C
    ViewController D -> ViewController E

I am currently inside ViewController E . I would like to dismiss all the modally presented view controllers so that I can get back to ViewController A . It seems that whatever I try on ViewController E , ( dismissViewController() with self , parentViewController , presentViewController , navigationController ) I am easily able to dismiss ViewController E but cannot dismiss ViewController B and all of its sub view controllers. It seems to me that I have lost all references to it yet when ViewController E is dismissed, ViewController D (a sub view controller of the navigation controller NavigationController B) is still visible.

Any help to dismiss both ViewController E and NavigationController B and all of its sub view controllers would be very much appreciated.

What you're looking for is called an unwind segue. I'm unaware of any way to do it outside of a storyboard but essentially you would set an unwind segue from ViewControllerE to ViewControllerA and then when in E , perform a segue as you would normally: self.performSegue('UnwindSegue', sender: self)

This is a pretty good article on the whole topic. Essentially what happens is the view controller stack unwinds all the way to the exit point of the unwind segue once its triggered so you can go from E to A no problem. You can also have multiple unwind segues through the app, just make sure to give them identifiers.

This is really useful in scenarios like logout, where the user might be five or six view controllers deep but your app has a side menu that they can use to log out at any point. You can just unwind all the way to the first view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM