Programmatically set the initial view controller using Storyboards

前端 未结 22 1924
感情败类
感情败类 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:23

    In AppDelegate.swift you can add the following code:

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewController(withIdentifier: "YourViewController_StorboardID")
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible()
    

    Of course, you need to implement your logic, based on which criteria you'll choose an appropriate view controller.

    Also, don't forget to add an identity (select storyboard -> Controller Scene -> Show the identity inspector -> assign StorboardID).

提交回复
热议问题