【问题标题】:Can't push view controller in AppDelegate with TabBarController无法使用 TabBarController 在 AppDelegate 中推送视图控制器
【发布时间】:2018-12-19 23:05:19
【问题描述】:

我有一个 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
    }

【问题讨论】:

  • 你的代码没问题。你现在得到什么?你设置TabBarControllerviewControllers正确吗?
  • 您忘记将导航控制器设置为TabBarController.viewControllers

标签: ios swift


【解决方案1】:

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

 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

【讨论】:

  • 实际上使用这段代码我可以推送 ViewController 但是没有导航栏,因为我们在代码开头禁用了它。我们能做什么?
  • 感谢您的快速回复。它拯救了我的一天。
【解决方案2】:

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

正确的解决方案是在UITabBarController中使用UINavigationController作为标签:

let tabBarController = TabBarController()
tabBarController.viewControllers = [UINavigationController(rootViewController: vc1), UINavigationController(rootViewController: vc2)]
window?.rootViewController = tabBarController

然后推送给他们:

let navigationController = tabBarController.selectedViewController as? UINavigationController
navigationController?.pushViewController(notificationViewController)

或者您可以创建一个新的视图控制器并将其设置为窗口的rootViewController:

window?.rootViewController = notificationViewController

但这需要更多导航代码在关闭通知等后设置回 tabBarController。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多