从AppDelegate访问UINavigationController

时间:2017-11-09 12:21:12

标签: swift swift3 uinavigationcontroller appdelegate

我需要访问UINavigationController

中的AppDelegate

这是我试过的

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()
}

这似乎不起作用。

如何从UINavigationController访问AppDelegate

感谢。

更新

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

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

2 个答案:

答案 0 :(得分:3)

试试这个:

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

答案 1 :(得分:1)

你可以试试这个

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

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

相关问题