在SWIFT中从AppDelegate获取ViewController实例

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (50)

当用户单击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线上崩溃了。

提问于
用户回答回答于

你可以试试:

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)

扫码关注云+社区