首页 > 解决方案 > 从 AppDelegate 调用 navigationController

问题描述

现在我正在实现一个信号通知,当用户单击通知时,我想打开特定的 Viewcontroller。

这是我在 AppDelegate 的代码

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let detailBrand = storyBoard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
detailBrand.getValue = value
navigationController?.pushViewController(detailBrand, animated: true)

如果我将它放在任何 ViewController 类中但在 AppDelegate 中却没有,则此代码可以正常工作。

请帮忙!!!

标签: swift4onesignal

解决方案


好吧,首先你必须明白Appdelgate 不是 a UIViewController

因此您不能使用pushViewController(detailBrand, animated: true),因为您并不完全UIVewController 能够这样做,但是您可以将其UINavigatetionController设置为您的根,然后从中推送。

你的代码应该是这样的

let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
rootViewController.pushViewController(profileViewController, animated: true)

另请记住,您应该将其放在didFinishLaunchWithOption方法中。

func application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool

推荐阅读