如何使用 Swift、iOS 7 设置 rootViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/24316966/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the rootViewController with Swift, iOS 7
提问by Stephen Fox
I want to set the rootViewController in the app delegate ..
我想在应用程序委托中设置 rootViewController ..
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
     var rootView: MyRootViewController = MyRootViewController()
     //Code to set this viewController as the root view??
     return true
}
回答by fruechtemuesli
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:
如果您正在使用故事板并希望以编程方式设置您的 rootViewController,请首先确保 ViewController 在 Identity Inspector 中具有故事板 ID。然后在 AppDelegate 中执行以下操作:
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
}
回答by Connor
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
}
回答by A.G
Swift 2.0:
斯威夫特 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
  }
回答by Aleksandr Movsesyan
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.
为了让它显示出来,如果您不使用故事板,您需要做一些事情。在函数应用程序内的 AppDelegate 内。
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
}
回答by Thiago Arreguy
   if let tabBarController = self.window!.rootViewController as? UITabBarController
    {
        tabBarController.selectedIndex = 0
    }

