简体   繁体   中英

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

This is code in 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:

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.

If you're within a NavigationController you can also use the answer of @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

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 {

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)

pop 2 view controllers navigationController?.popViewControllers(viewsToPop: 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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