我正在使用Xcode 11和Swift 5。
[收到APNS通知后,我需要从AppDelegate跳到故事板深处的视图控制器。此viewController(chatVC)位于选项卡和导航控制器以及其他几个视图控制器的后面。参见下图。我知道如何检查标签的通知以及如何在AppDelegate中使用launchOptions触发跳转。但是我正在为如何为最后一个视图控制器建立上下文而苦苦挣扎,以便用户可以使用后退按钮一直回到选项卡栏控制器。
我已经阅读了很多SO答案,并尝试了许多方法,但是似乎没有一个选项卡控件中的nav控制器具有相同的嵌入。这是我在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( {$0.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
,如果需要的话。
希望这对您有用!