0

Is there a way to change root controller when someone is midway into using the app? The way I have it set up right now is that if a new user uses the app they will go into a login page that has the embedded navigation controller. When the user fills out the information they go onto the next page to confirm. The embedded navigation is to go back and change any info. Once they confirm they go to the main page. Now the issue is that the app goes to a different view that already has a Navigation Controller when the user returns and follows some logic. How do I assign the returning user navigation controller when the new user gets into that section? When the user presses back while they are on the blank view they will go back to the confirmation page. enter image description here

How do I have the confirmation page view get the navigation controller on the top and not the bottom? I'm not sure where to change the Root Navigation Controller but I assume it should be in the prepareForSegue in Confirmation View Controller

if segue.identifier == "showMemoryTable" {
             let memoryListVC = segue.destination as? ReturningUserCityDetailTableViewController
            memoryListVC?.userData = userData
            if userData?.newUser == true {
                let newRootViewController = ReturningUserCityTableViewController()
                self.navigationController?.viewControllers.first = newRootViewController
                let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let viewController: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "homeVC") 
                navigationController?.setViewControllers([viewController], animated: true)

            }

I am not sure if I should use setViewControllers but I believe so

LampPost
  • 856
  • 11
  • 30

1 Answers1

0

If you are aiming to Change the root view controller, you should not do it by by performing a segue, it would be more appropriate to do it like this:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newRootViewController = mainStoryboard.instantiateViewController(withIdentifier: "ReturningUserCityTableViewController") as! ReturningUserCityTableViewController()
let ad = UIApplication.shared.delegate as! AppDelegate
ad.window?.rootViewController = newRootViewController

Note that it changes the whole app root view controller; You could set the desired navigation controller to be the root of the app (obviously you could get them by their storyboard ID).

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Maybe I am not understanding clearly, because I am still learning iOS, but this isn't what I need no? I am not trying to change the root view controller of the entire app but how the stack goes for the UINavigationController. https://stackoverflow.com/questions/33374272/swift-ios-set-a-new-root-view-controller – LampPost Feb 25 '18 at 05:16