使用标签栏和导航实例化深度视图控制器

Instantiating deep View Controller with Tab bar and Navigation

我正在使用 Xcode 11 和 Swift 5。

收到 APNS 通知后,我需要从 AppDelegate 跳转到故事板深处的视图控制器。这个 viewController (chatVC) 在标签栏和导航控制器以及其他几个视图控制器后面。请参见下图。我知道如何检查标签的通知以及如何在 AppDelegate 中使用 launchOptions 来触发跳转。但我正在努力解决如何为最后一个视图控制器建立上下文,以便用户可以使用后退按钮一直返回到选项卡栏控制器。

我已经阅读了很多 SO 答案,并尝试了很多方法,但 none 似乎在标签栏控制器中嵌入了相同的导航控制器。这是我在 AppDelegate 中的代码(在读取通知中的标签后):

        if tag == "CS" {
            // Set up a Chat View Controller.
            if let chatVC = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: Constants.Storyboard.chatVC) as? NewChatViewController,
                let tabBarVC = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: Constants.Storyboard.tabBarController) as? UITabBarController,
                let csVC = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: Constants.Storyboard.csViewController) as? CustomerServiceViewController,
                let helpVC = UIStoryboard(name:"Main", bundle: .main).instantiateViewController(withIdentifier: Constants.Storyboard.helpVC) as? HelpViewController
            {
                // Set the customer service document Id.
                chatVC.cs = cs
                // Make the tabBarVC the Root View Controller
                self.window?.rootViewController = tabBarVC
                self.window?.makeKeyAndVisible()
                // Select the Favorites Index.
                tabBarVC.selectedIndex = 0
                // Push the Customer Service VC on top.
                tabBarVC.show(csVC, sender: Any?.self)
                // Push the Help VC on top of Customer Service VC.
                csVC.show(helpVC, sender: Any?.self)
                // Push the the chat detail page on top.
                helpVC.show(chatVC, sender: Any?.self)
            }
        }
    }
    return true

我该怎么做才能跳转到 chatVC 并在其下方设置导航上下文以便可以使用后退按钮?

以下是您的操作方法:

guard let tabBarVC = UIApplication.shared.windows.filter( {[=10=].rootViewController is UITabBarController } ).first?.rootViewController as? UITabBarController else { return }
tabBarVC.selectedIndex = 0 //you can select another tab if needed
guard let chatVC = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: Constants.Storyboard.chatVC) as? NewChatViewController else { return }
if let navController = tabBarVC.viewControllers?[0] as? UINavigationController {
   navController.pushViewController(chatVC, animated: true)
}

您还可以在推送之前将通知中的对象传递到您的 chatVC,以备不时之需。

希望这对你有用!