swift - 进入前台后呈现视图

swift - present view after entering foreground

我有一个带有登录视图的应用程序。在 appDelegate 中,我检查用户是否使用密码锁定了应用程序。如果他这样做,他会转到登录视图。 (在 didFinishLaunchingWithOptions 中调用的代码)

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier(Views.LoginVC.rawValue)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()

登录后,我在不同的导航控制器中显示实际内容。

let vc = self.storyboard?.instantiateViewControllerWithIdentifier(Views.MainVC.rawValue)
let rootVC = UINavigationController(rootViewController: vc!)
self.presentViewController(rootVC, animated: false, completion: nil)

关闭应用再次进入时,appDelegate会再次检查用户是否使用了密码。如果是,则再次调用第一个代码(我检查过,代码被调用)(从 applicationWillEnterForeground 调用的代码)。但是他没有进入登录视图,而是停留在主要内容视图。如果他再次离开,控制台会弹出一条错误消息。

2016-04-25 20:50:52.714 Point[7222:123809] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

我需要更改什么,以便应用程序在返回时再次显示登录视图?我该如何避免此错误消息?

更新

我刚刚通过从 applicationDidBecomeActive 而不是 didFinishLaunchingWithOptions 调用代码解决了我的问题。我不知道为什么会这样,但它有效!

不,我认为你有误。您应该将您的代码放在 applicationWillEnterForeground() 中,因为您希望每次用户运行该应用程序时都调用一次您的代码。

应用程序 "inactive" 仍在屏幕上,但未处于活动状态。例如,如果您双击主页按钮,或者如果您收到警报。每次应用程序从非活动状态转换为活动状态时都会调用方法 applicationDidBecomeActive()