简体   繁体   中英

Navigation controller how to pop entire stack, including root

I am learning coordinator pattern. I have a couple of log in view controllers. Once user finished log in, I want to pop the entire navigation stack and set my main view controller to be the root. Is it doable?

A workaround I have is to set a empty view controller to be the root, so I can do popToRootViewController and then add main vc as a child. But this doesn't look clean.

If you want to completely remove and replace the whole stack, just start the flow again.

Setup a new navigation controller and AppCoordinator and assign the navigation controller to window.rootViewController. Call start on the AppCoordinator just like you would at startup and you have a new stack.

You could add a method onto your AppCoordinator to handle this for you

class AppCoordinator: Coordinator, NSObject {

    var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    func start() {
       let vc = MainViewController()
       navigationController.setViewControllers([vc], animated: true)
    }

    func resetNavigationStack() {

        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }

        navigationController = UINavigationController()
        appDelegate.window = navigationController
        start()
    }

}

Change the viewControllers property

 let vc = ///
 self.navigationController?.viewControllers = [vc]   

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