简体   繁体   English

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

[英]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文档initpush所写)。 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.

相关问题 无法从AppDelegate推送到View Controller而不会丢失导航栏和标签栏设置iOS - Can't push to View Controller from AppDelegate without losing Navigation Bar and Tab Bar setup iOS 从AppDelegate,Swift导航到TabBarController内部的特定View Controller - Navigate to a specific View Controller inside a TabBarController from AppDelegate, Swift iOS - 无法使用TabbarController显示推送视图控制器 - iOS - Cannot Push View Controller with TabbarController Showing 从AppDelegate和Tab Bar推送View Controller - Push View Controller From AppDelegate and Tab Bar 如何通过推送通知使用TabBarController导航显示视图控制器 - How to show view controller with TabBarController navigation from push notification 使用Swift从推送通知appdelegate更改视图控制器中UIWebView的URL - change URL of UIWebView in view controller from push notification appdelegate with Swift 如何以TabBarController为根推送视图 - How to Push A view with TabBarController as Root 没有登录PFUser时无法从AppDelegate重定向到视图控制器 - Can't redirect to view controller from AppDelegate when there's no logged in PFUser 处于自身功能时无法推送视图控制器 - Can't push view controller while in its own function 无法从Web View Controller加载推入URL - Can't load a push url from my Web View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM