24

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
9
  • where do u decide if user is logged in or not ? App delegate ? view did load ? Commented Nov 11, 2015 at 22:58
  • yes in app delegate. code edited. Commented Nov 11, 2015 at 23:10
  • 1
    you can change to other navigation controller too, use storyboard ID !!! Commented Nov 11, 2015 at 23:11
  • when user is log by pressing the log button, I can't make self.window.rootViewController because i'm not in the app delegate now but in the logInViewController Commented Nov 11, 2015 at 23:13
  • u can use the app delegate any where in your view controllers. Just the pass the reference of app delegate to your view controller!!!! Commented Nov 11, 2015 at 23:14

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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. :)

2 Comments

then how u dismiss it ?
U mean when u want to log out ? Btw u can also use nsuserdefaults to check login
0

enter image description here

It's not the best approach, but it works.✅

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    AppRouter.shared.setAppFlow(.onboarding, animated: false)
    return true
}

Manager:

self.window.rootViewController = Your type of UINavigationController

class AppRouter {

enum FlowType {
    case onboarding
    case app
}

static let shared = AppRouter()

var window: UIWindow = UIWindow(frame: UIScreen.main.bounds)

func setAppFlow(_ type: FlowType, animated: Bool) {
    
    self.withTransition(animated)
    
    switch type {
    case .onboarding:
        self.window.rootViewController = UINavigationController(rootViewController: TestViewController())
    case .app:
        self.window.rootViewController = UINavigationController(rootViewController: MainTableViewController())
    }
    self.window.makeKeyAndVisible()
}

private func withTransition(_ bool: Bool) {
    guard bool else { return }
    let transition = CATransition()
    transition.duration = 0.3
    transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
    transition.type = .push
    transition.subtype = .fromRight
    transition.fillMode = .both
    self.window.layer.add(transition, forKey: kCATransition)
}
}

Use for change:

@objc func myButtonAction() {
    AppRouter.shared.setAppFlow(.app, animated: true)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.