Reputation: 1790
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
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
Upvotes: 24
Views: 10923
Reputation: 55
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)
}
Upvotes: 0
Reputation: 323
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. :)
Upvotes: 4
Reputation: 40030
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.
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.
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.
Upvotes: 7