Sharing Property information between classes in Obj C

69 views Asked by At

I've been trying to share information from one of my classes to another one (trying out something for Spritebuilder) and it's just. Not. Working.

I want to change the text of the label in the second class to a string I define in the first. Here is my code.

MainScene.h

@interface MainScene : CCNode
@property CCLabelTTF *lblChange;

-(void) _button;

@end

MainScene.m

#import "MainScene.h"
#import "StoryScene.h"
@implementation MainScene

-(void)_button {
    StartScene *startHold = [[StartScene alloc] init];
    [startHold.lblTwo setString:@"Hello World!"];
    NSLog(@"%@, this is the StoryScene", startHold);
    NSLog(@"%@, this is the Main Scene", @"Yessss");
    NSString *filler = [startHold.lblTwo string];
    NSLog(@"%@",filler);
    CCScene *storyScene = [CCBReader loadAsScene:(@"StartScene")];
    [[CCDirector sharedDirector] replaceScene: storyScene];
}
@end

StoryScene.h

#import "CCNode.h"

@interface StartScene : CCNode
@property CCLabelTTF *lblTwo;

@end

StoryScene.m

#import "StoryScene.h"
@implementation StoryScene
@end

I've tried making StoryScene a property in the MainScene class, synthesizing pretty much everything, moving things around - but I cannot get that dogging lblTwo to log as anything, which prevents the Label text from changing (I think). Here is the relevant output I get in my log.

TheCaveOfMan[58721:1593076] <StartScene = 0x7fc2cfb31e40 | Name = (null)>, this is the StoryScene
2015-06-18 15:52:04.712 TheCaveOfMan[58721:1593076] Yessss, this is the Main Scene
2015-06-18 15:52:04.712 TheCaveOfMan[58721:1593076] <StartScene = 0x7fc2cfb31e40 | Name = (null)>, this is the label
2015-06-18 15:52:04.713 TheCaveOfMan[58721:1593076] (null)
2015-06-18 15:52:04.714 TheCaveOfMan[58721:1593076] CCBReader: Couldn't find member variable: lblTwo
2015-06-18 15:52:04.725 TheCaveOfMan[58721:1593076] cocos2d: surface size: 640x1136

I don't know why it can't find the lblTwo variable, and I don't know how to change the StoryScene's logging name to something that isn't (null), or how to give the lblTwo string something to hold. Help?

1

There are 1 answers

2
user1251560 On

At the second line, where you try to assign "Hello World", I think the lblTwo property might be uninitialised (so nil). You need to alloc and init the property before calling setString on it.