Ios Can';使用TabBarController在AppDelegate中推送视图控制器

Ios Can';使用TabBarController在AppDelegate中推送视图控制器,ios,swift,Ios,Swift,我有一个TabBarController作为我的应用程序的rootViewController。当用户点击通知时,我尝试推送视图控制器。但代码不起作用。如何在没有故事板的情况下从AppDelegate推送视图控制器 AppDelegate.swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptions

我有一个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
然后在内部
didReceiveMemotentification

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

self.navigationController?.isNavigationBarHidden = false

您可以尝试将选项卡嵌入导航中

 let nav = UINavigationController(rootViewController: TabBarController())
 nav.isNavigationBarHidden = true
 window?.rootViewController = nav
然后在内部
didReceiveMemotentification

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

self.navigationController?.isNavigationBarHidden = false

不应将UITabBarController嵌入UINavigationController中(如和中所述)。然而,它正在发挥作用

正确的解决方案是将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)
或者,您可以创建一个新的视图控制器,并将其设置为Windows的rootViewController:

window?.rootViewController = notificationViewController

但这需要更多的导航代码来在取消通知等后设置tabBarController。

您不应该将UITabBarController嵌入UINavigationController中(如和中所述)。然而,它正在发挥作用

正确的解决方案是将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)
或者,您可以创建一个新的视图控制器,并将其设置为Windows的rootViewController:

window?.rootViewController = notificationViewController

但这需要更多的导航代码,以便在取消通知等后设置tabBarController。

您的代码没有问题。你现在得到了什么?您是否正确设置了
TabBarController
viewControllers
?您忘记将导航控制器设置为
TabBarController。viewControllers
您的代码很好。你现在得到了什么?您是否正确设置了
TabBarController
viewControllers
?您忘了将导航控制器设置为
TabBarController.viewControllers
实际上,使用此代码,我可以推送ViewController,但没有导航栏,因为我们在代码开头禁用了它。我们能做什么?谢谢你的快速回复。实际上,通过这段代码,我可以推送ViewController,但没有导航栏,因为我们在代码开头禁用了它。我们能做什么?谢谢你的快速回复。它救了我一天。