22

In my app, if the user isn't logged, it shows a login controller which is embedded in a navigation controller. When the user is logged, the app should switch to the other navigation controller to display the app.

How can I switch from one navigation controller to another one when the user is logged. ?

Thanks

enter image description here

I'm checking if the user is log in app delegate :

 // Check if user is log
    let currentUser = PFUser.currentUser()
    if currentUser != nil {
        // Do stuff with the user
    } else {
        // Show the signup or login screen
        let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let nav = mainStoryboardIpad.instantiateViewControllerWithIdentifier("LogInController") as! UINavigationController
        self.window?.rootViewController = nav
    }

SOLUTION : looks like it works
When user press logIn button :

let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let nav = mainStoryboardIpad.instantiateViewControllerWithIdentifier("MainNavController") as! UINavigationController
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window?.rootViewController = nav
user3722523
  • 1,740
  • 2
  • 15
  • 27

2 Answers2

6

Solution 1

One solution would be to use just a single navigation controller. When the user logs in, you would pop all the view controllers used for logging in and push main view controller on the stack.

Solution 2

Alternatively, you could present a new navigation controller modally on top of the login stuff, with main controller as its root. It would be simply presented on top of it.

Solution 3

You can also consider creating the navigation controller with main view controller first and presenting the login navigation controller on top of it. Then, when user logs in you would just dismiss the login navigation controller revealing the main view controller.

Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • Wouldn't solution 1 involve a glitch after you pop the login VCs and before pushing the main VC? We have a similar problem but with the added twist of the second nav controller (i.e., flow) needing a different orientation from the first nav controller. – Crashalot Jan 05 '17 at 17:35
4

Set your navigation controller storyboard ID

    let navigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SecondNavigationController")
    self.presentViewController(navigationController, animated: true, completion: nil)

Hope this helps. :)

Amorn Narula
  • 323
  • 3
  • 13