从 AppDelegate 调用 navigationController

Call navigationController from AppDelegate

现在我正在实施一个信号通知,当用户点击通知时我想打开特定的 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 Class 中,它都可以正常工作,但在 AppDelegate 中却不行。

请帮忙!!!

嗯,首先你要明白 Appdelgate 不是 UIViewController

因此你不能使用 pushViewController(detailBrand, animated: true) 因为你不在 UIVewController 中才能这样做,但是你可以启动 UINavigatetionController 将其设置为你的root 然后从中推送。

你的代码应该是这样的

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