单个函数关闭所有打开的视图控制器 [英] single function to dismiss all open view controllers
问题描述
我有一个应用程序,它是一个单视图应用程序.我有一个导航控制器链接到根视图控制器的所有子控制器.
I have an app which is a single view application. I have a navigation controller linked up to all child controllers from the root view controller.
在每个子控制器中,我都有一个注销按钮.我想知道我是否可以有一个可以调用的函数,该函数将关闭一路上打开的所有控制器,无论用户按下注销时当前打开的是哪个控制器?
In each child controller, I have a logout button. I'm wondering if I can have a single function I can call which will dismiss all the controllers which have been open along along the way, no matter which controller was currently open when the user presses logout?
我的基本开始:
func tryLogout(){
self.dismissViewControllerAnimated(true, completion: nil)
let navigationController = UINavigationController(rootViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginViewController") )
self.presentViewController(navigationController, animated: true, completion: nil)
}
我正在寻找执行此任务的最节省内存的方法.我会将我的注销功能放在一个单独的 utils 文件中,但是我不能使用 self.而且我仍然无法知道要动态关闭哪些控制器.
I am looking for the most memory efficient way of carrying out this task. I will put my logout function in a separate utils file, but then I can't use self. And I still have the issue of knowing which controllers to dismiss dynamically.
更新已建议弹出到根视图控制器.所以我的尝试是这样的:
Update Pop to root view controller has been suggested. So my attempt is something like:
func tryLogout(ViewController : UIViewController){
print("do something")
dispatch_async(dispatch_get_main_queue(), {
ViewController.navigationController?.popToRootViewControllerAnimated(true)
return
})
}
这是实现我所追求的最佳方式吗?
Would this be the best way to achieve what I'm after?
推荐答案
您可以拨打:
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
应该关闭根视图控制器之上的所有视图控制器.
Should dismiss all view controllers above the root view controller.
这篇关于单个函数关闭所有打开的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!