为什么AppDelegate中的窗口为nil

Fro*_*art 12 cocoa-touch ios swift

我需要从中呈现视图控制器AppDelegate,所以我编写了以下代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let authViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as ViewController
if let keyWindow = UIApplication.sharedApplication().keyWindow {
    keyWindow.rootViewController = authViewController
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,windowkeyWindow都是nil.为什么?

jrt*_*ton 41

如果不使用主界面选项,则需要自己创建窗口:

window = UIWindow(frame:UIScreen.mainScreen().bounds)
Run Code Online (Sandbox Code Playgroud)

Swift 3.0

self.window = UIWindow(frame: UIScreen.main.bounds)
Run Code Online (Sandbox Code Playgroud)

然后使用,调用上面的代码window.

最后,打电话makeKeyAndVisible()给窗口.

  • 我不想自动加载初始视图控制器,因为我需要手动提供授权视图控制器或主视图控制器 (2认同)