Search code examples
iosswiftuinavigationcontroller

How to remove a ViewController from navigation stack and not have the option to go back to it-Swift


I have a Login page and if the login is successful the user is taken to a landing viewController(VC). I have a navigationController. I pop the login VC and push in the landing VC. The problem I am having is that the login VC persists in the stack as indicated by the left button on the navigation controller which navigates you back to the login page. How do I remove the login VC copletely from the stack so that the navigation controller can not navigate a user back to it once they have logged in?

 self.navigationController?.popViewController(animated: true)

 let landingPage = self.storyboard?.instantiateViewController(withIdentifier: "landingPage") as! 
 LandingViewController

 self.navigationController?.pushViewController(landingPage, animated: true)

Solution

  • You can use the setViewControllers method to set the stack like this:

    self.navigationController?.setViewControllers([landingPage], animated: true)
    

    You don't need to pop the login view controller first either.