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
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
}