在我的Xcode项目中,当用户点击通知时,我想先将其发送到tabBar中的某个项目,然后再实例化视图控制器并将对象发送到该视图控制器。我有将它们发送到所需的tabBar的代码,但我不知道如何在将tabBar和导航栏保持与视图控制器连接的同时将它们实例化到视图控制器。关于此问题的所有答案都需要您更改根视图控制器,这使我在调用视图控制器时失去了与tabBar和导航栏的连接。
一个真实的例子: 用户收到Instagram通知,说“ John开始关注您”->用户点击通知-> Instagram打开并显示通知选项卡->快速将用户发送到“ John”个人资料,并在用户按下后退按钮时,它将它们发送回通知标签
应该知道:我首先要进入某个选项卡的原因是要获取该选项卡的导航控制器,因为我要使用的视图控制器没有该控制器。
这是我将用户发送到“通知”选项卡的工作代码(我添加了注释,使其与Instagram示例类似,以便更好地理解):
if let tabbarController = self.window!.rootViewController as? UITabBarController { tabbarController.selectedViewController = tabbarController.viewControllers?[3] //goes to notifications tab if type == "follow" { //someone started following current user //send to user's profile and send the user's id so the app can find all the information of the user } }
首先,您需要使TabBarController满足需要:
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil) let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
然后充分满足所有viewControllersTabBarController的需求。如果您的viewControllers嵌入到UINavigationController?如果是这样,您将改为使用导航控制器:
viewControllers
UINavigationController
let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController
另外,您也应该实例化所需的ViewController:
let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController
viewControllers从TabBarController开始制作所有NavigationControllers :
tabBarController.viewControllers = [first, second, third]
并检查:这取决于您的选择。
if tabBarController.selectedViewController == first { // Option 1: If you want to present first.present(desiredVC, animated: true, completion: nil) // Option 2: If you want to push first.pushViewController(desiredVC, animated. true) }
将tabBarController设置为rootViewController:
rootViewController
self.window = UIWindow.init(frame: UIScreen.main.bounds) self.window?.rootViewController = tabBarController self.window?.makeKeyAndVisible()
最后:这是您完成的代码:
func openViewController() { let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil) let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController tabBarController.viewControllers = [first, second, third] if tabBarController.selectedViewController == first { // Option 1: If you want to present first.present(desiredVC, animated: true, completion: nil) // Option 2: If you want to push first.pushViewController(desiredVC, animated. true) } self.window = UIWindow.init(frame: UIScreen.main.bounds) self.window?.rootViewController = tabBarController self.window?.makeKeyAndVisible() }
如果要在点击通知时显示或推送ViewController?尝试这样的事情:
extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { switch response.actionIdentifier { case UNNotificationDefaultActionIdentifier: openViewController() completionHandler() default: break; } } }