从App Delegate实例化嵌入在TabBarController中的View Controller

时间:2018-10-29 18:23:36

标签: swift uitabbarcontroller instantiation appdelegate

我希望用户点击推送通知后导航到视图控制器。该视图控制器嵌入在标签栏控制器中,并且需要从该响应中接收数据。如果不必传递数据,则只需将标签栏控制器称为根视图控制器即可。但是,由于我想传递一个名为returnFromNotification的布尔值,因此需要执行以下操作:

userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    if response.notification.request.identifier == "LocalPushReminder" {

        let storyboard = UIStoryboard(name: "Main", bundle:nil)
        let tabbarController = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! UITabBarController

        if let practiceVC = tabbarController.viewControllers?[1] as? PracticeCollectionViewController {
            practiceVC.returnedFromNotification = true
            self.window?.rootViewController = practiceVC
            self.window?.makeKeyAndVisible()
        }
    }
    completionHandler()
}

视图已加载,但是未嵌入在选项卡栏中,并且丢失了所有数据。 谢谢!

0 个答案:

没有答案