简体   繁体   中英

Get Instance Of ViewController From AppDelegate In Swift

I am trying to load a specific ViewController from the app delegate in swift when a user clicks a UILocalNotification. I have figured out that this is called in this function:

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

But when I try and access one of the open ViewControllers I think it's returning null because my application is crashing. Here is what I am trying:

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()

It's crashing on the popToViewController line.

You could try:

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)

Swift 3:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM