简体   繁体   English

从AppDelegate访问UINavigationController

[英]Access UINavigationController From AppDelegate

I need to access UINavigationController in AppDelegate 我需要在AppDelegate访问UINavigationController

Here is what I tried 这是我尝试过的

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

This doesn't seem to work. 这似乎不起作用。

How do I access UINavigationController from the AppDelegate ? 如何从AppDelegate访问UINavigationController

Thanks. 谢谢。

Update 更新资料

I am using this library to display the notification in the application when the app is active. 当应用程序处于活动状态时,我正在使用此在应用程序中显示通知。 this library requires an instance of navigationController to display the notification. 该库需要一个NavigationController实例来显示通知。

When I use this code in AppDelegate the notification is not displayed, but when I use in the in ViewController the notifications are displayed. 当我在AppDelegate使用此代码时,不会显示通知,但是当我在ViewController使用时,会显示通知。

尝试这个:

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

You can try this 你可以试试这个

let navigationController = application.windows.first?.rootViewController as? 让NavigationController = application.windows.first?.rootViewController为? UINavigationController UINavigationController

it wont crash even if your rootViewController is TabBarController . 即使您的rootViewController是TabBarController,它也不会崩溃。 it will safely unwrap it . 它会安全地打开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM