简体   繁体   中英

What happens to root view controller in ios / swift

Say I am switching viewcontrollers manually like this How to transition to a new view controller with code only using Swift

What happens to the (root) controller when another viewcontroller is brought to front. Can I just reinstantiate it or should I keep a reference to it pu i to front?

No you don't have to re-instantiate it. You can access all the current UIViewControllers from the UINavigationController like this

self.navigationController?.viewControllers

This will return an [UIViewController] . If you want to get the viewController it is at index = 0 . If you want to get back to the root you can call

self.navigationController?.popToRootViewControllerAnimated(true)

I would've added this as a comment, just that I don't have enough points. To answer your question - you should not have to worry about re-instantiating the previous View Controller. If you are using the presentViewController:animated:completion method, you can use the presentedViewController and presentingViewController properties from the UIViewController class that are set after the transition.

The developers link below will help you decide better on how to handle View Controllers transition depending on your use case -

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/presentedViewController

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