[英]Can't push view controller in AppDelegate with TabBarController
I have a TabBarController as a rootViewController for my app. 我有一个TabBarController作为我的应用程序的rootViewController。 And I'm trying to push view controller when user clicks to notification.
而且我正在尝试在用户单击通知时推送视图控制器。 But code isn't working.
但是代码不起作用。 How can I push view controller from AppDelegate without storyboards.
如何在没有情节提要的情况下从AppDelegate推送视图控制器。
AppDelegate.swift AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = TabBarController()
window?.makeKeyAndVisible()
return true
}
You can try to embed the tab inside a navigation 您可以尝试将标签嵌入导航中
let nav = UINavigationController(rootViewController: TabBarController())
nav.isNavigationBarHidden = true
window?.rootViewController = nav
Then inside didReceiveRemoteNotification
然后在
didReceiveRemoteNotification
内部
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(////
}
to show nav inside the vc viewDidLoad
在vc
viewDidLoad
显示导航
self.navigationController?.isNavigationBarHidden = false
You should not embed UITabBarController in a UINavigationController (as written in Apple Documentation init and push ). 您不应该将UITabBarController嵌入UINavigationController中(如Apple文档init和push所写)。 However, it's working.
但是,它正在工作。
The correct solution is to use UINavigationController as tabs in UITabBarController: 正确的解决方案是将UINavigationController用作UITabBarController中的选项卡:
let tabBarController = TabBarController()
tabBarController.viewControllers = [UINavigationController(rootViewController: vc1), UINavigationController(rootViewController: vc2)]
window?.rootViewController = tabBarController
and then push to them: 然后推向他们:
let navigationController = tabBarController.selectedViewController as? UINavigationController
navigationController?.pushViewController(notificationViewController)
Or you can create a new view controller and settng it as a rootViewController of window: 或者,您可以创建一个新的视图控制器并将其设置为window的rootViewController:
window?.rootViewController = notificationViewController
But this requires more navigation code to setting back tabBarController after dismissing notification etc. 但这需要更多导航代码才能在取消通知等之后重新设置tabBarController。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.