从推送通知打开时未调用ViewController .viewDidLoad

时间:2018-11-17 19:07:41

标签: ios swift

在故事板中,我有一个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函数,并且一切正常。好像是比赛条件。

0 个答案:

没有答案