Ios 其视图不在窗口层次结构中的LoginViewController!代表
我的目标是检查钥匙链中是否有令牌,如果没有,则只显示登录视图控制器屏幕。现在的问题是,我得到了这个错误。我在AppDelegate.swift中编写了此代码 .LoginViewController:0x7ff59b619820>其视图不在窗口层次结构中 这是密码Ios 其视图不在窗口层次结构中的LoginViewController!代表,ios,objective-c,swift,Ios,Objective C,Swift,我的目标是检查钥匙链中是否有令牌,如果没有,则只显示登录视图控制器屏幕。现在的问题是,我得到了这个错误。我在AppDelegate.swift中编写了此代码 .LoginViewController:0x7ff59b619820>其视图不在窗口层次结构中 这是密码 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
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)
有关更多信息,请参见
在出现之前添加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)
有关更多信息,请参见,方法是将视图控制器设置为UINavigationControllers rootviewcontroller,然后将navigationcontroller添加为Windows rootviewcontrollers,以查看下面修改的代码:
func showLoginScreen() -> Void {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as UIViewController
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
}
如果您想根据登录状态在两个ViewController之间切换,我建议您遵循以下方法:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
if LofinStatus == true{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("My_Offer") as UIViewController
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
}else{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Login", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as UIViewController
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
}
self.window!.makeKeyAndVisible()
注意:在上述代码中,我使用了两个故事板Main和Login
它对我非常有效,请尝试一下并告诉我结果。感谢您将视图控制器设置为UINavigationControllers rootviewcontroller,然后将navigationcontroller添加为Windows rootviewcontrollers将起作用查看下面修改的代码:
func showLoginScreen() -> Void {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as UIViewController
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
}
如果您想根据登录状态在两个ViewController之间切换,我建议您遵循以下方法:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
if LofinStatus == true{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("My_Offer") as UIViewController
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
}else{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Login", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as UIViewController
let navigationController = UINavigationController(rootViewController: vc)
self.window!.rootViewController = navigationController
}
self.window!.makeKeyAndVisible()
注意:在上述代码中,我使用了两个故事板Main和Login
它对我非常有效,试试看,告诉我结果。谢谢