小编典典

在Swift中从AppDelegate获取ViewController的实例

swift

当用户单击UILocalNotification时,我试图迅速从应用程序委托中加载特定的ViewController。我已经知道这是在此函数中调用的:

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!)

但是当我尝试访问一个打开的ViewController时,我认为它返回null,因为我的应用程序崩溃了。这是我正在尝试的:

var rootViewController = self.window!.rootViewController
var storyBoard = rootViewController.storyboard
var setViewController = storyBoard.instantiateViewControllerWithIdentifier("CurrentShows") as ViewController_CurrentShows

rootViewController.navigationController.popToViewController(setViewController, animated: false)
setViewController.reloadData()

在popToViewController行上崩溃了。


阅读 435

收藏
2020-07-07

共1个答案

小编典典

您可以尝试:

let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows") as! DetailViewController
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)

斯威夫特3:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = mainStoryboard.instantiateViewController(withIdentifier: "viewController")
self.present(viewController, animated: true, completion: nil)
2020-07-07