Skip to content

Snippet: Xcode 11 – select Initial View Controller from the Storyboard Programmatically

In a recent post I covered how, in xCode 11 & iOS13, to programatically set up the initial view controller without the use of a Storyboard. A recent forum question prompted me to adopt this code to work with Storyboards. This is useful when you need control over which screen to use for the initial view controller or need to do other processing prior to opening it. Much of the logic is the same as the other post, so I’m just posting the code here for future reference…

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

   var window: UIWindow?
   let storyboard = UIStoryboard(name: "Main", bundle: nil)

   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      guard let windowScene = scene as? UIWindowScene else { return }
      let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
      window = UIWindow(windowScene: windowScene)
      window?.rootViewController = vc
      window?.makeKeyAndVisible()
   }

// rest of the usual SceneDelegate methods...

Leave a Reply