How to set the rootViewController with Swift, iOS 7

后端 未结 5 2161
暗喜
暗喜 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
    }
    
    0 讨论(0)
  • 2020-12-05 18:17

    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
      }
    
    0 讨论(0)
  • 2020-12-05 18:19

    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
    }
    
    0 讨论(0)
  • 2020-12-05 18:21
       if let tabBarController = self.window!.rootViewController as? UITabBarController
        {
            tabBarController.selectedIndex = 0
        }
    
    0 讨论(0)
  • 2020-12-05 18:37

    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
    }
    
    0 讨论(0)
提交回复
热议问题