Swift: Setting rootViewController not working?

前端 未结 1 1041
旧时难觅i
旧时难觅i 2021-01-25 04:11

I\'m trying to start a new Swift project. This is my first time trying to create the views programatically. However it doesn\'t even look like my controller is being loaded? All

相关标签:
1条回答
  • 2021-01-25 04:28

    In the line:

    self.window?.rootViewController = self.rootViewController()
    

    If the window property is nil, it will not execute your self.rootViewController() call. You can read more about calling methods with optional chaining in the documentation for details.

    If you are trying to create your initial user interface in code, you will need to create a UIWindow instance and assign it to self.window. This is done for you automatically when using a Storyboard.

    Disclaimer: I haven't written this code in a few versions of iOS, so this may not be exactly correct, but it will get you going in the right direction:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let applicationFrame = UIScreen.mainScreen().applicationFrame
        let window = UIWindow(frame: applicationFrame)
        window.rootViewController = self.rootViewController()
        window.makeKeyAndVisible()
        self.window = window
    
        return true
    }
    
    0 讨论(0)
提交回复
热议问题