在故事板中,我有一个HomeViewNavigationController
,其根视图控制器为HomeViewController
。我还有其他一些视图控制器和一个名为PushViewController
的视图控制器。像这样:
HomeViewNavigationController ---(root view)--> HomeViewController --> AViewController --> BViewController --> PushViewController
当我的应用程序未运行并且收到推送消息并且我想打开HomeViewController
时,只需初始化HomeViewNavigationController
并将其设置为rootViewController
即可,如下所示:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let homeViewNav = storyboard.instantiateViewController(withIdentifier: "HomeViewNavController") as? UINavigationController
self.window?.rootViewController = homeViewNav
completionHandler()
}
但是我想打开PushViewController
并将后退按钮链接到HomeViewController
。所以我这样做:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let homeViewNav = storyboard.instantiateViewController(withIdentifier: "HomeViewNavController") as? UINavigationController
let pushView = storyboard.instantiateViewController(withIdentifier: "PushViewController") as! PushViewController
homeViewNav.pushViewController(pushView, animated: true)
self.window?.rootViewController = homeViewNav
completionHandler()
}
问题是我的.ViewDidLoad
中的HomeViewController
函数从未被调用,而我的.viewWillDisappear
函数却被调用了。结果,当我尝试在.viewWillDisappear
中处置从未在.viewDidLoad
中初始化的变量时,我的应用程序崩溃了。
如何将pushViewController
压入堆栈,但要确保HomeViewController
正确初始化
仅供参考:这似乎有时发生,而不是一直发生。有时会调用.viewDidLoad
函数,并且一切正常。好像是比赛条件。