Programmatically set the initial view controller using Storyboards

前端 未结 22 2371
感情败类
感情败类 2020-11-22 15:00

How do I programmatically set the InitialViewController for a Storyboard? I want to open my storyboard to a different view depending on some condition which may

22条回答
  •  鱼传尺愫
    2020-11-22 15:12

    If you prefer not to change applicationDidFinish, you can do the following trick:

    Set Navigation controller as an initial view controller and assign to it a custom class 'MyNavigationController'. Then you can tweak its root view controller during viewDidLoad - it will override the root view controller that you set in your storyboard.

    class MyNavigationController: UINavigationController {
        override func viewDidLoad() {
            super.viewDidLoad()
            if !isLoggedIn() {
                viewControllers = [R.storyboard.authentication.loginView()!]
            }
        }
    
        private func isLoggedIn() -> Bool {
            return false
        }
    
    }
    

提交回复
热议问题