ios - 关闭所有打开的 View Controller 的单一功能

标签 ios swift uiviewcontroller dismiss

我有一个应用程序,它是一个单一 View 应用程序。我有一个导航 Controller 链接到 Root View Controller 的所有子 Controller 。

在每个子 Controller 中,我都有一个注销按钮。我想知道我是否可以有一个我可以调用的函数,它将关闭一路上打开的所有 Controller ,无论用户按下注销时当前打开的是哪个 Controller ?

我的基本开始:

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.我仍然不知道要动态关闭哪些 Controller 。

更新 已建议弹出到 Root View Controller 。所以我的尝试是这样的:

func tryLogout(ViewController : UIViewController){
     print("do something")
     dispatch_async(dispatch_get_main_queue(), {
         ViewController.navigationController?.popToRootViewControllerAnimated(true)
         return
     })
 }

这是实现我所追求目标的最佳方式吗?

最佳答案

你可以打电话:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

应该关闭 Root View Controller 之上的所有 View Controller 。

关于ios - 关闭所有打开的 View Controller 的单一功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33520899/

相关文章:

ios - UILabel 位置与预期不同

ios - 以编程方式在 Swift 中为 iOS 制作带有名称首字母的图像,例如 Gmail

ios - CloudKit - 哪些 CKError 应该作为 .partialFailure 处理,它们是否也应该作为非部分故障处理?

ios - 我如何模糊我的 UIViewController 上的一些层(不是代码,只是概念)

iphone - 为什么 NSDateFormatter 在 12 小时时间设置的 24 小时本地上返回 NULL?

Expo 生成的 iOS 构建(ipa 文件)太大

ios - NSNotificationCenter 并在 View Controller 和我的类之间传递数据/我走错了路吗?

iphone - 无法推送新的 View Controller

ios - UISplitViewController,重用DetailViewController(Swift)

ios - 解析 XML 时如何获取特定元素?