繁体   English   中英

无法使用TabBarController在AppDelegate中推送视图控制器

[英]Can't push view controller in AppDelegate with TabBarController

我有一个TabBarController作为我的应用程序的rootViewController。 而且我正在尝试在用户单击通知时推送视图控制器。 但是代码不起作用。 如何在没有情节提要的情况下从AppDelegate推送视图控制器。

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
    }

您可以尝试将标签嵌入导航中

 let nav = UINavigationController(rootViewController: TabBarController())
 nav.isNavigationBarHidden = true
 window?.rootViewController = nav

然后在didReceiveRemoteNotification内部

if let nav = self.window?.rootViewController as? UINavigationController {
    nav.pushViewController(////
}

在vc viewDidLoad显示导航

self.navigationController?.isNavigationBarHidden = false

您不应该将UITabBarController嵌入UINavigationController中(如Apple文档initpush所写)。 但是,它正在工作。

正确的解决方案是将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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM