ios - 登录其 View 不在窗口层次结构中的 ViewController!在 Appdelegate 中

标签 ios objective-c swift

我的目标是检查钥匙串(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/

相关文章:

iOS 绘制简单的条形图像 Mint 应用程序

php - NSString 中的转义字符

ios - 扩展 NSError 是一个好习惯吗

objective-c - AFJSONRequestOperation 结果中的键周围缺少引号吗?

ios - Alamofire responseJSON 无法将 NSDictionary 类型的值转换为自定义对象

swift - 值类型变量在传递给函数时如何被复制,这个副本是什么?

ios - 带图像的 UIView

iphone - 从 tableview 获取数据并将其存储到 sqlite 数据库

ios - 以方形格式保存照片

objective-c - 初始化之前使用的变量注释