从Swift中的AppDelegate获取ViewController的实例

时间:2014-07-08 17:45:34

标签: ios swift appdelegate

我正在尝试在用户单击UILocalNotification时从swift中的app delegate加载特定的ViewController。我已经发现在这个函数中调用了它:

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

但是当我尝试访问其中一个打开的ViewControllers时,我认为它因为我的应用程序崩溃而返回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行崩溃了。

1 个答案:

答案 0 :(得分:25)

你可以尝试:

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)