Sephiroth
Sephiroth

Reputation: 21

I'm using swift. I cannot pass my variable to another scene

I've been trying to pass my variable to a another scene. Previously when I was using single view application I was able to pass my variable usingoverride func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) and had no problem. Now I'm using game with SpriteKit and I am unable to use the same function it says method does not override any method from its superclass. When I take out the override it doesn't give me any errors but it does not pass the variable. Im also using the latest Xcode version and I'm not use Main.storyboard

override func prepareForSegue(segue: UIStoryboardSegue, sender:   AnyObject?) {
    var DestResults : GameOver = segue.destinationViewController as!GameOver

    DestResults.gameOverScore = self.score
}

enter image description here

Upvotes: 1

Views: 640

Answers (2)

Epic Defeater
Epic Defeater

Reputation: 2147

Based on the comment, I can tell that your superclass isn't a UIViewController. SKScene doesn't implement prepareForSegue, so you can't use prepareForSegue in an SKScene.

You could declare a struct outside of the class declaration. Like this.

struct Variables {
  static var aVariable = //Whatever you want it to equal
} 

You could access the variable like so.

Variables.aVariable

You could try editing the variable right before the transition to the next scene. And then access it the next scene like above.

Upvotes: 0

BaseZen
BaseZen

Reputation: 8718

You're confused about the object hierarchy. There is always a view controller that presents content, whether or not it has been built automatically for you.

The hierarchy is:

UIWindow: fills the device screen, and contains a root view controller

The root view controller is a UIViewController.

The UIViewController contains a root view (UIView), which in this case is an SKView.

The SKView uses the SKScene instance as a delegate to describe what its scene should look like.

You're implementing SKScene here. You need to look at the structure of your App and traverse to the right level of the object hierarchy. Or, you need to work within the game scene, and design a 'Game Over' scene, rather than an entire 'Game Over' view controller. Segues only apply between View Controllers, not game scenes.

Upvotes: 0

Related Questions