21

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
PretzelJesus
  • 869
  • 3
  • 11
  • 21

1 Answers1

27

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)
gotnull
  • 26,454
  • 22
  • 137
  • 203
  • 1
    I tried using this inside a function and it didn't work, I'm using xcode7 and swift2. do you mind updating the code above please. – suisied Oct 05 '15 at 22:50
  • @suisied Might be worth creating a question on StackOverflow and linking to it as we can't really diagnose your issue without seeing your code. – gotnull Oct 06 '15 at 23:03
  • not working in swift 2.. impossible to get "Main" storyboard. – Async- Oct 31 '15 at 00:10