简体   繁体   English

快速返回根视图控制器

[英]Back to root view controller in swift

Is there any way to dismiss all the view controllers ( in which some controllers are pushed through navigation) and go back to the root view controller.有没有办法关闭所有视图控制器(其中一些控制器通过导航推送)并返回到根视图控制器。 I saw lots of examples but they didn't work in my application.我看到了很多示例,但它们在我的应用程序中不起作用。 I am using swift 4我正在使用 swift 4

This is code in appdelegate这是 appdelegate 中的代码

func setNavigationToRootViews(){

    storyBoard = UIStoryboard(name: "Main", bundle: nil)
    nav = storyBoard?.instantiateViewController(withIdentifier: "mainNavigation") as! UINavigationController?
    let accessToken: String? = KeychainWrapper.standard.string(forKey: "token")
    print(accessToken as Any)

   if accessToken != nil {

        let homeVc = storyBoard?.instantiateViewController(withIdentifier: "Home-VC") as! HomeViewController
        nav?.pushViewController(homeVc, animated: false)
    }else{

        let welcomVc = storyBoard?.instantiateViewController(withIdentifier: "login-VC") as! LoginViewController
        nav?.pushViewController(welcomVc, animated: false)
    }

    let leftMenuVC = storyBoard?.instantiateViewController(withIdentifier: "menu-VC") as! MenuViewController
    container = MFSideMenuContainerViewController.container(withCenter: nav, leftMenuViewController: leftMenuVC, rightMenuViewController: nil)
    container?.panMode = MFSideMenuPanModeNone
    window?.rootViewController = container
    window?.makeKeyAndVisible()

}

and this in my last View controller这是我最后一个视图控制器

@IBAction func okayBtnTapped(_ sender: Any) {


        _ = self.navigationController?.popToRootViewController(animated: 
          true)
        dismiss(animated: true, completion: nil)

    }

Try this one:试试这个:

self.navigationController?.popToRootViewController(animated: true)

You can just set a new RootController like this:您可以像这样设置一个新的 RootController:

let storyboard = UIStoryboard(name: "sName", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<YOUR ROOT CONTROLLER>")
self.window?.rootViewController = viewController

If you don't have a window at self.window you need to instanciate one based on your AppDelegate.如果您在self.window上没有窗口,则需要根据您的 AppDelegate 实例化一个窗口。

If you're within a NavigationController you can also use the answer of @Anshul Bhatheja如果您在NavigationController中,您还可以使用@Anshul Bhatheja 的答案

Check you navigation controller, and view controllers inside that navigation controller.检查您的导航控制器,并查看该导航控制器内的控制器。 It seems debugging issue.似乎是调试问题。

this method popToRootViewController(animated:) is the method should work这个方法 popToRootViewController(animated:) 是应该工作的方法

If you are working with simple push navigation controller and you want to go back to root view controller then you simply have to write如果你正在使用简单的推送导航控制器并且你想回到根视图控制器那么你只需要写

_ = self.navigationController?.popToRootViewController(animated: true)

extension UINavigationController {扩展 UINavigationController {

func popToViewController(ofClass: AnyClass, animated: Bool = true) {
    if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
        popToViewController(vc, animated: animated)
    }
}

func popViewControllers(viewsToPop: Int, animated: Bool = true) {
    if viewControllers.count > viewsToPop {
        let vc = viewControllers[viewControllers.count - viewsToPop - 1]
        popToViewController(vc, animated: animated)
    }
}

} }

pop to SomeViewController class navigationController?.popToViewController(ofClass: SomeViewController.self)弹出到 SomeViewController 类navigationController?.popToViewController(ofClass: SomeViewController.self)

pop 2 view controllers navigationController?.popViewControllers(viewsToPop: 2)弹出 2 个视图控制器navigationController?.popViewControllers(viewsToPop: 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM