ap1*_*123 2 xcode uiviewcontroller ios appdelegate swift
在我的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
}
}
Run Code Online (Sandbox Code Playgroud)
首先,你要发布一个TabBarController:
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
Run Code Online (Sandbox Code Playgroud)
然后给所有viewControllers
的TabBarController 充电.如果你的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
Run Code Online (Sandbox Code Playgroud)
此外,您还应该实例化您想要的ViewController:
let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController
Run Code Online (Sandbox Code Playgroud)
viewControllers
从TabBarController生成所有NavigationControllers :
tabBarController.viewControllers = [first, second, third]
Run Code Online (Sandbox Code Playgroud)
并检查:这是你的选择.
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)
}
Run Code Online (Sandbox Code Playgroud)
将tabBarController设为rootViewController
:
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)
最后:这是你完成的代码:
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()
}
Run Code Online (Sandbox Code Playgroud)
如果要在点击通知时显示或推送ViewController?尝试类似的东西:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
openViewController()
completionHandler()
default:
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1103 次 |
最近记录: |