This approach worked for me and I think it is correct. So far this is the easiest and fastest way to pass data between scenes which I found.
1. Basic informations about userData:
1.1. Hold the apple "Command key" and press the left mouse button (in Xcode) on the "userData" string (for example. "self.userData ;").
You will get the following info:
// An optional dictionary that can be used to hold user data pretaining to the node. Defaults to nil.
@property (SK_NONATOMIC_IOSONLY, retain) NSMutableDictionary *userData;
1.2. Visit the apple docs:userData
1.3 userData is an NSMutableDictionary which is basically an special array. Beside the value it contains a key which is bind to the value. With this key you can find values.
This might be helpful aswell:apple docs:NSMuttableDictionary
2. The solution:
2.1. firstScene
//somewhere in firstScene.m
//how the tranisiton from firstScene to secondScene is going to look and how long it is goint to take
SKTransition *reveal = [SKTransition moveInWithDirection:SKTransitionDirectionDown duration:0.5];
SKView * skView = (SKView *)self.view;
SKScene *secondScene = [WBMGameEndsScene sceneWithSize:skView.bounds.size];
//You need to initialize the NSMD since it is by default nil.
secondScene.userData = [NSMutableDictionary dictionary];
Here I am refering to the userData of the secondScene by adding a object which is my score . For test purpose it is just 6. I will add my score instance variable. You can add what ever you want since it accepts objects of type "id". The key is important. You will use it in the secondScene.m to access the object value since. Check that you dont type it wrong.
[secondScene .userData setObject:@"6" forKey:@"score"];
//Testing
NSLog(@"Is it finally working -- %@",[secondScene .userData objectForKey:@"score"]);
//The secondScene will scale to fit the whole SKView
secondScene.scaleMode = SKSceneScaleModeAspectFill;
//present the secondScene
[self.scene.view presentScene: secondScene transition:reveal];
*WBMGameEndsScene is my secondScene. I created it in Xcode with "File - New File ..." as :SKScene . It will contain the userData shown later on.
2.2. secondScene
//secondScene.m ( ex. in my :"@implementation WBMGameEndsScene")
These next lines of code check if the value has been correctly added:
-(void)didMoveToView:(SKView *)view
{
NSLog(@"-- -(void)willMoveFromView:(SKView *)view --");
NSLog(@"Working score is : %@",[self.userData valueForKey:@"score"]);
NSLog(@"Working score is : %@",[self.userData objectForKey:@"score"]);
}
Console output is:
2014-03-12 15:29:06.804 AppTest[4841:60b] -(id)initWithSize:(CGSize)size
2014-03-12 15:29:06.806 AppTest[4841:60b] scoreLabel
2014-03-12 15:29:06.812 AppTest[4841:60b] successMessage
2014-03-12 15:29:06.815 AppTest[4841:60b] Is it finally working -- 6
2014-03-12 15:29:06.815 AppTest[4841:60b] -- -(void)willMoveFromView:(SKView *)view --
2014-03-12 15:29:06.816 AppTest[4841:60b] Working score is : 6
2014-03-12 15:29:06.816 AppTest[4841:60b] Working score is : 6
If I got something wrong please correct me :).
Specs: iOS 7.0 ,Xcode Version 5.1 (5B130a), OS X 10.9.2