yongwoo
yongwoo

Reputation: 75

When should I set rootViewController? it's very confused

Hi I am struggling with rootViewController I left some code fragment with an explanation below, please let me know.

If I do it like below, it works and every things fine.

    private func presentLogin() {
        log.info("presenting login..")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        vc.modalPresentationStyle = .fullScreen
        appDelegate.window!.rootViewController = vc
        present(vc, animated: false)
    }

right after that if I execute the code below, ios shows nothing but white blank page..

    private func presentMain() {
        log.info("presenting main..")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! MainTabBarController
        vc.modalPresentationStyle = .fullScreen
        appDelegate.window!.rootViewController = vc
        present(vc, animated: false)
    }

but when I deleted the code

appDelegate.window!.rootViewController = vc

everything is fine.

In other words, the code below works only the first time.

appDelegate.window!.rootViewController = vc

why? what am I missing?, I don't understand..

Upvotes: 0

Views: 25

Answers (1)

Andreas Oetjen
Andreas Oetjen

Reputation: 10199

It seems there are a lot of bugs concerning the exchange of the root view controller. Switching the root view controller is also a little uncommon way of "navigation". I would recommend a different approach:

  • Upon app startup, use a "launch" view controller. This is your root view controllers, and it stays to be your root view controller all the time
  • if login is required, present a login view controller
    • after successful login, dismiss the login view and go on
  • if logged in, present the main application's entry view

Upvotes: 1

Related Questions