【问题标题】:Access UINavigationController From AppDelegate从 AppDelegate 访问 UINavigationController
【发布时间】:2017-11-09 12:21:12
【问题描述】:

我需要在AppDelegate中访问UINavigationController

这是我尝试过的

func applicationDidBecomeActive(_ application: UIApplication) {
    let syncManager = SyncManager()
    let navigationController: UINavigationController = UIStoryboard(
        name: "Main",
        bundle: nil
    ).instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
    syncManager.setNavigationController(navigationController)
    syncManager.sync()
}

这似乎不起作用。

如何从AppDelegate 访问UINavigationController

谢谢。

更新

当应用程序处于活动状态时,我正在使用此 library 在应用程序中显示通知。这个库需要一个 navigationController 的实例来显示通知。

当我在AppDelegate 中使用此代码时,不会显示通知,但是当我在ViewController 中使用时,会显示通知。

【问题讨论】:

  • 有错误提示吗?
  • 没有错误信息,但也不能正常工作。基本上我正在使用需要 navigaitonController 实例的通知显示。当我使用此代码时,通知根本不显示。但是当我直接使用 viewController 中的 navigaitonController 实例时,它可以工作。让我更清楚地更新我的问题。
  • 一个标准的UINavigationController 实际上没有UIViewController。它只能导航和管理他们的viewControllers
  • UIWindowrootViewController 不适合您吗?您的视图层次结构如何?

标签: swift swift3 uinavigationcontroller appdelegate


【解决方案1】:

试试这个:

let navigationController = application.windows[0].rootViewController as! UINavigationController

【讨论】:

    【解决方案2】:

    你可以试试这个

    让 navigationController = application.windows.first?.rootViewController 为? UINavigationController

    即使你的 rootViewController 是 TabBarController 也不会崩溃。它会安全地打开它。

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多