2

Context

This question is related to Swift: SKSpriteKit, using Storyboards, UIViewController and UIButton to set in game parameters? and uses the same M.W.E. (link below).

Taking a look at the Main.storyboard we see the following: enter image description here

While this storyboard would allow one to progress from left to right by clicking:

play -> (easy/hard) -> GameScene

UIViewController -> MyUIViewController -> GameViewController

and from right to left by clicking the UIButton on the last UIViewController with the UILabel stating "Score", there is not a way to get from GameViewController to the last UIViewController with the "Score" UILabel.

Question

Given a certain event occurring during the GameScene, how can I pass some information (e.g. a score) from the GameScene and transition to the last last UIViewController so that the user can start the game over again. In addition to deleting everything in the GameViewController as to not just end up with unreachable old instances of the GameScene.

For the certain condition, let that be tapping the purple sprite in the M.W.E. For the information, let that be something trivial like the time until the sprite was pressed, or even just a random number.

Minimum Working Example

Stack Overflow SKSpriteKit

Note

My answer here - and implemented hereStackOverflow SKSpriteKit with Menu - shows that this is clearly possible, but by relegating everything to SKViews rather than using a storyboard. So if possible, please keep answers related to using this hybrid storyboard-SKView approach and how to have the GameScene call up the "Score"-UIViewController.

Community
  • 1
  • 1
SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • Are you familiar with the delegate pattern? It is the usual way for child views to communicate with a parent. This [SO Post](http://stackoverflow.com/questions/39561177/how-can-i-indicate-a-view-which-has-created-with-uikit-from-a-skscene/39564948#39564948) shows the basic idea and performs the unwind seque. The delegate protocol could be extended to include a score property, so the scene can notify it's ViewController when the value changes. – Mark Brownsword Jan 24 '17 at 08:07
  • @MarkBrownsword Nope. I am not, could you possible explain it with this M.W.E.: https://github.com/SumNeuron/StackOverflow-SKSpriteKit-with-Menu – SumNeuron Jan 24 '17 at 08:14

1 Answers1

1

You could use the userData instance property of your next scene to store the current scene, something like:

nextScene.userData = NSMutableDictionary()
nextScene.userData?.setObject(actualScore, forKey: "actualScore" as NSCopying)

and when you are in the next scene you can ask to this NSMutableDictionary who is the previous scene as:

if let actualScore = self.userData?.value(forKey: "actualScore") {
    print("well done!, the score was: \(actualScore)")
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133