How to set the rootViewController with Swift, iOS 7

后端 未结 5 2164
暗喜
暗喜 2020-12-05 17:59

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

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDicti         


        
5条回答
  •  独厮守ぢ
    2020-12-05 18:11

    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
    }
    

提交回复
热议问题