I have a TabBarController as a rootViewController for my app. 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.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
}
答案 0 :(得分:1)
You can try to embed the tab inside a navigation
let nav = UINavigationController(rootViewController: TabBarController())
nav.isNavigationBarHidden = true
window?.rootViewController = nav
Then inside didReceiveRemoteNotification
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(////
}
to show nav inside the vc viewDidLoad
self.navigationController?.isNavigationBarHidden = false
答案 1 :(得分:1)
您不应将UITabBarController嵌入UINavigationController中(如Apple Documentation init和push所述)。但是,它正在工作。
正确的解决方案是将UINavigationController用作UITabBarController中的选项卡:
let tabBarController = TabBarController()
tabBarController.viewControllers = [UINavigationController(rootViewController: vc1), UINavigationController(rootViewController: vc2)]
window?.rootViewController = tabBarController
然后将其推向它们:
let navigationController = tabBarController.selectedViewController as? UINavigationController
navigationController?.pushViewController(notificationViewController)
或者您可以创建一个新的视图控制器并将其设置为window的rootViewController:
window?.rootViewController = notificationViewController
但这需要更多导航代码才能在取消通知等之后重新设置tabBarController。