1

I have one layer called alayer, and there is a button called abutton, when click the button, another layer called blayer will show in alayer, not replaceScene, please look at the following code,

alayer.m

-(void)abuttonclicked:(id)sender
{
  blayer *blayer = [blayer node];
  blayer.position = ccp(1,1);
  [self addChild:blayer];
}

blayer.m has a button called bbutton and string value called bstring, I want to click the b button, it will close blayer (remove blayer from alayer), and pass the string value bstring to alayer, please look at following code,

 -(void)bbuttonclicked:(id)sender
 {
  // how can do here to close its self(remove its self from alayer), and pass the bstring to alayer?
 }

thanks.

ps. I can use NSUserDefault to pass the string value, but I think it's a bad way to do this, is there another way to pass value?

yegomo
  • 297
  • 2
  • 11

2 Answers2

0

maybe to pass the string you could declare and implement a property in ALayer.h/.m

 @property(nonatomic,copy) NSString *stringFromLayerB;

to remove bLayer when bButtonClicked :

 -(void)bbuttonclicked:(id)sender
 {
     ALayer *lay = (ALayer*) self.parent;
     lay.stringFromLayerB = @"Whatever you want to set";
     [self removeFromParentAndCleanup:YES];
 }

There are other ways to do this. You could implement a callback mechanism , use notifications, some kind of delegate protocol binding BLayer and ALayer. All depends what your real (unsaid) requirements are.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
0

Considering your scenario, I think its better to use NSNotificationCenter. You can post notification from blayer when the button is tapped and add an observer in alayer to respond exactly how to want.

Add [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"BlayerNotification" object:nil]; in alayer's init and [[NSNotificationCenter defaultCenter] removeObserver:self]; in its dealloc.

Its selector like:

- (void)receivedNotification:(NSNotification *)notification {
    NSString *string = (NSString *)notification.object;
    NSLog (@"String received %@", string);
}

Now you can post notification from blayer when hte button is clicked:

-(void)bbuttonclicked:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BlayerNotification" object:self];
    [self removeFromParentAndCleanup:YES];
}
nomann
  • 2,257
  • 2
  • 21
  • 24