单个函数来关闭所有打开的视图控制器 [英] single function to dismiss all open view controllers

查看:114
本文介绍了单个函数来关闭所有打开的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它是一个单一的视图应用程序。我有一个导航控制器链接到根视图控制器中的所有子控制器。

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?.dismissViewControllerAnimated(false, completion: nil)

应解除根视图控制器上方的所有视图控制器。

Should dismiss all view controllers above the root view controller.

这篇关于单个函数来关闭所有打开的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆