我的目标是检查钥匙串(keychain)中是否有 token ,如果没有则简单地显示登录 View Controller 屏幕。现在的问题是,我得到了这个错误。我在 AppDelegate.swift
.LoginViewController: 0x7ff59b619820> on whose view is not in the window hierarchy!
这是代码
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)
}
最佳答案
试试这个
add
self.window.makeKeyAndVisible()
before present
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
关于ios - 登录其 View 不在窗口层次结构中的 ViewController!在 Appdelegate 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37316247/