我的目标是检查密钥链中是否有令牌,如果没有,则只显示登录视图控制器屏幕。现在的问题是,我得到了这个错误。我在AppDelegate.swift
中编写了这段代码
.LoginViewController:0x7ff59b619820>其视图不在窗口层次结构中!
这是密码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let keychain = Keychain(server: "https://app.herokuapp.com", protocolType: .HTTPS)
if ((try? keychain.contains("token")) != nil) {
showLoginScreen()
} else {
}
return true
}
func showLoginScreen() -> Void {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
let rootViewController = self.window!.rootViewController
rootViewController?.presentViewController(setViewController, animated: false, completion: nil)
}
最佳答案
试试这个
在出现前加上
self.window.makeKeyAndVisible()
更新
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
self.window.makeKeyAndVisible()
self.window!.rootViewController.presentViewController(setViewController, animated: false, completion: nil)
有关更多信息,请参见this