简体   繁体   中英

Swift - How to keep reference to old view controller when instantiating new view controller?

I am trying to programmatically instantiate a new view controller of the same type as my original view controller using this code:

self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier(StoryboardKeys.ViewIdentifier), animated: true)

I would like the user to be able to go back to my original view controller, so I was wondering if there is a way to keep the old view controller in memory? Also if I instantiate a view controller this way, do I get to prepareforsegue (in order to programmatically add a back button)? If not, how could I add a back button when pushing this view controller?

I suspect there is a better way to do this, so if there is, please let me know. Thank you very much for any help!

UINavigationController maintain your view controllers as a stack automatically, so don't worry about your view controller's memory management. Only if you want a custom back button, otherwise you don't have to add a back button by yourself, everything has been done.

Just use self.navigationController!.popViewControllerAnimated(true) somewhere in your second view controller . Navigation controller contains a stack of views that you can retrieve by using self.navigationController!.viewControllers . You can modify this stack as you like.

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