MLyck
MLyck

Reputation: 5765

Swift Changing Navigation ViewController stack (programatically)

I'm having some trouble with the logic in my application.

I have been looking for a way to change the navigation controller's viewcontroller stack.

For several reasons.

One, I have a login and sign up page, which each have buttons pushing to the other view controller. Creating an /infinite/ loop being able to go between them.

I also need to change the topViewController, to my dashboard_VC after the user has logged in. So the back button to the login page doesn't show up...

I've been trying to find a solution for a while. I've mainly been looking at rootViewController, until I realized that's probably not the one I'm looking for since my rootViewController is my navigationController and not my first View Controller in the stack.

So I tried to change the stack with this:

self.navigationController?.setViewControllers([LandingPageVC(),LoginVC()], animated: false)

However this causes an infinite loop. Since It runs every time this viewController is loaded, and when it's run, it seems to reload all the viewcontrollers...

I tried putting it in viewDidAppearAnimated instead. But same results. I've since then been trying to put this outside the viewDidLoad or Appear methods. but I can't get it to work since the class doesn't have "navigationController".

Any help would be greatly appreciated!

(I'm not using storyboards)

Upvotes: 1

Views: 2335

Answers (1)

ericgu
ericgu

Reputation: 2249

In your transitioning to ViewController (Dashboard_VC):

    override init() {
        super.init()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }  

In your transitioning from Login View Controller:

if (successfulLogin) {    
    let mainController = DashBoardViewController()
    mainController.navigationItem.setHidesBackButton(true, animated: false)
    navigationController!.pushViewController(mainViewController, animated: false)
}    

Upvotes: 2

Related Questions