ios 关闭所有打开的视图控制器的单个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33520899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:10:58  来源:igfitidea点击:

single function to dismiss all open view controllers

iosswiftuiviewcontrollerdismiss

提问by user2363025

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?

在每个子控制器中,我都有一个注销按钮。我想知道我是否可以调用一个函数来关闭所有沿途打开的控制器,无论用户按下注销时当前打开的是哪个控制器?

My basic start:

我的基本开始:

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)
}

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.

我正在寻找执行此任务的最有效的内存方式。我会将我的注销功能放在一个单独的 utils 文件中,但是我不能使用 self. 而且我仍然有知道要动态解雇哪些控制器的问题。

UpdatePop to root view controller has been suggested. So my attempt is something like:

已建议将 Pop更新为根视图控制器。所以我的尝试是这样的:

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?

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

回答by Liam

You can call :

您可以致电:

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

Should dismiss all view controllers above the root view controller.

应该关闭根视图控制器上方的所有视图控制器。

回答by Bijender Singh Shekhawat

Updated answer for Swift 4and swift 5

Swift 4Swift 5 的更新答案

UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)

and when you use navigationController

当你使用 navigationController

self.navigationController?.popToRootViewController(animated: true)

回答by Naishta

Swift 4:

斯威夫特 4:

To dismiss any unwanted residue Modal ViewControllers, I used this and worked well without retaining any navigation stack references.

为了消除任何不需要的残留物 Modal ViewControllers,我使用了它并且在不保留任何导航堆栈引用的情况下运行良好。

UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil)

self.view.window!did crash for my case possibly because its a Modal screen and had lost the reference to the window.

self.view.window!我的情况确实崩溃了,可能是因为它是一个模态屏幕并且丢失了对窗口的引用。

回答by markhorrocks

Swift3

斯威夫特3

navigationController?.popToRootViewControllerAnimated(true)

回答by Nitin Alabur

Take a look at how unwind segues work. Its super simple, and lets you dismiss/pop to a certain viewcontroller in the heirarchy, even if it consists of a complex navigation (nested pushed and or presented view controllers), without much code.

看看 unwind segue 是如何工作的。它非常简单,即使它由复杂的导航(嵌套的推送和/或呈现的视图控制器)组成,也可以让您关闭/弹出到层次结构中的某个视图控制器,无需太多代码。

Here's a very good answer (by smilebot) that shows how to use unwind segues to solve your problem https://stackoverflow.com/a/27463286/503527

这是一个非常好的答案(来自smilebot),它展示了如何使用展开segues来解决您的问题 https://stackoverflow.com/a/27463286/503527

回答by Pardeep Kumar

works for Swift 3.0 +

适用于 Swift 3.0 +

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

回答by Yun CHEN

If you have a customed UITabbarController, then try dismiss top viewController in UITabbarController by:

如果您有自定义的 UITabbarController,请尝试通过以下方式关闭 UITabbarController 中的 top viewController:

class MainTabBarController: UITabBarController {

    func aFuncLikeLogout() {

        self.presentedViewController?.dismiss(animated: false, completion: nil)

        //......
    }

}

回答by Rahul Panzade

SwiftUsed this to jump directly on your ROOT Navigation controller.

Swift使用它直接跳转到您的 ROOT 导航控制器。

self.navigationController?.popToRootViewController(animated: true)

self.navigationController?.popToRootViewController(animated: true)