简体   繁体   English

iOS / Swift中的根视图控制器会发生什么

[英]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 说我像这样手动切换视图控制器如何仅使用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 您可以像这样从UINavigationController访问所有当前的UIViewControllers

self.navigationController?.viewControllers

This will return an [UIViewController] . 这将返回一个[UIViewController] If you want to get the viewController it is at index = 0 . 如果要获取viewController,则它位于index = 0 If you want to get back to the root you can call 如果您想回到root ,可以致电

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. 要回答您的问题-您不必担心重新实例化以前的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. 如果使用的是presentViewController:animated:completion方法,则可以使用过渡后设置的UIViewController类中的presentedViewControllerpresentingViewController属性。

The developers link below will help you decide better on how to handle View Controllers transition depending on your use case - 下面的开发人员链接将帮助您根据用例更好地决定如何处理View Controllers转换-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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