我有一个应用程序,它是一个单一 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/