9

I have been trying to perform a segue from within a sprite kit SKScene layer. My application requires the user to touch an image on the scene that would push up a new UIViewController. But every time I try to perform from within the SKScene game layer, it gives me output with has no segue with identifier error. I do have a segue and have specified it with an identifier.

My simple question, how can one perfrom segue with a SKScene game layer running? Please help me out.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Arjun Busani
  • 550
  • 1
  • 7
  • 15

2 Answers2

10

Segues belong to view controllers, not SpriteKit scenes. If you want to perform a segue from within a SpriteKit node's event handling code, you'll need to look up the view controller that owns the view presenting the scene. For example, in a SKScene subclass' touchesBegan:withEvent: method:

UIViewController *vc = self.view.window.rootViewController;
[vc performSegueWithIdentifier:@"id" sender:nil];
rickster
  • 124,678
  • 26
  • 272
  • 326
  • this didn't work for me, I had to use the answer here: http://stackoverflow.com/questions/27053436/segue-in-skscene-to-uiviewcontroller – Rachel Harvey Jun 22 '16 at 17:45
  • @RachelHarvey: Sorry, the code in my answer is just one example of how to get a VC you can segue from. What you actually need is specifically a view controller that owns the scene and has the segue you're looking for — if your `SKScene` is presented in a view controller that's within a tab view controller or navigation controller, the root VC probably isn't the one that has your segue. – rickster Jun 22 '16 at 19:55
4

Swift version :

self.view!.window!.rootViewController!.performSegueWithIdentifier("gameOverSegue", sender: self)
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
Alvin George
  • 14,148
  • 92
  • 64