1

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)

1 Answer 1

2

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) } 
Sign up to request clarification or add additional context in comments.

4 Comments

I added both to my project and it does what I wanted. However, I'm having a rough time seeing what the first section of code does? I took it out of my Dashboard_VC and I don't see a change. as for hiding the button, that only solves my problem on the dashboard and landing page. What about when I click "sign up" on my login screen. Or visa versa. Those screens still need back buttons, it should just go to the landingPage_VC instead of login or signup. Overall thank you so much for the help though, leaps ahead of where I was left of before.
Since your title says "Programmatically", I assumed you are not using Interface Builder so you will need init(coder aDecoder) to instantiate a new VC in our current version of Swift. The line to hide the Back Button is a method of the View Controller. It only hides for that View Controller. Typically, there is a seperate LogOut process, you don't go back from your main to a LoginVC, but customize as you need. themainthread.com/blog/2014/08/…
Thanks for the explanation ericgu! I really appreciate the help! You wouldn't happen to also know how to fix my login / signup problem?, Since those pages should still have back buttons, just always take you to the "Landingpage_VC"
I think you should ask a new question per StackOverFlow's guidelines. Very likely someone will help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.