从AppDelegate调用navigationController

时间:2018-10-17 08:11:36

标签: swift4 onesignal

现在,我正在实施onesignal通知,当用户单击通知时,我想打开特定的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内则不能,则此代码可以正常工作。

请帮助!!!

1 个答案:

答案 0 :(得分:1)

好吧,首先您必须了解Appdelgate不是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