27

I want to set the rootViewController in the app delegate ..

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

     var rootView: MyRootViewController = MyRootViewController()
     //Code to set this viewController as the root view??


     return true

}
Stephen Fox
  • 14,190
  • 19
  • 48
  • 52

5 Answers5

45

If you are using a storyboard and want to set your rootViewController programmatically, first make sure the ViewController has a Storyboard ID in the Identity Inspector. Then in the AppDelegate do the following:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

   // get your storyboard
   let storyboard = UIStoryboard(name: "Main", bundle: nil)

   // instantiate your desired ViewController
   let rootController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! UIViewController

   // Because self.window is an optional you should check it's value first and assign your rootViewController
   if let window = self.window {
      window.rootViewController = rootController
   }

   return true
}
orkoden
  • 18,946
  • 4
  • 59
  • 50
fruechtemuesli
  • 2,809
  • 3
  • 17
  • 15
  • 1
    i'm using Swift 2.0 and this minor change worked for me in storyboard `let rootController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as UIViewController` – SeeCoolGuy Jun 19 '16 at 18:17
32

You can do something like this.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

     var rootView: MyRootViewController = MyRootViewController()

     if let window = self.window{
            window.rootViewController = rootView
     }

     return true
}
Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • 5
    I just used this code on Xcode 7.1, and the compiler stated that since rootView is never mutated, it could be made a constant with the `let` keyword instead of `var`. – bneely Nov 07 '15 at 22:04
15

Swift 2.0:

var window: UIWindow?
 var storyboard:UIStoryboard?

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  window =  UIWindow(frame: UIScreen.mainScreen().bounds)
  window?.makeKeyAndVisible()

  storyboard = UIStoryboard(name: "Main", bundle: nil)
  let rootController = storyboard!.instantiateViewControllerWithIdentifier("secondVCID")

  if let window = self.window {
   window.rootViewController = rootController
  }
Alvin George
  • 14,148
  • 92
  • 64
12

In order to get it to show there are some things you need to do if you are not using a storyboard. Inside the AppDelegate inside the function application.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let frame = UIScreen.mainScreen().bounds
    window = UIWindow(frame: frame)

    let itemsViewControler: UITableViewController = BNRItemsViewController()
    if let window = self.window{
        window.rootViewController = itemsViewControler
        window.makeKeyAndVisible()
    }

    return true
}
  • 1
    while the storyboard answer does present the correct controller for me, this answer does fire but I end up with a black screen :-/ – SeeCoolGuy Jun 19 '16 at 18:20
0
   if let tabBarController = self.window!.rootViewController as? UITabBarController
    {
        tabBarController.selectedIndex = 0
    }
Thiago Arreguy
  • 2,739
  • 2
  • 19
  • 18