2013-10-20 2 views
1

나는 alayer라고하는 레이어가 하나 있는데 버튼을 클릭하면 abutton이라고하는 버튼이 있습니다. blayer라고하는 다른 레이어가 대체로 표시되지 않고 Scene을 표시합니다.cocos2d-iphone의 다른 레이어에 표시된 레이어를 닫는 방법

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

blayer.m이 bbutton 및 문자열 값라는 버튼을 bstring 호출 한 다음 코드,

alayer.m에, 나는 B 버튼을 클릭, 그것은 blayer을 닫습니다 (alayer에서 blayer을 제거), 문자열 값 bstring을 전달하여 다음 코드를 확인하십시오.

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

감사합니다.

ps. NSUserDefault를 사용하여 문자열 값을 전달할 수 있지만이 방법이 좋지 않다고 생각합니다. 값을 전달할 수있는 또 다른 방법이 있습니까?

답변

0

어쩌면 당신은 선언 할 수있는 문자열을 전달하고 ALayer.h의 속성을 구현하기 위해 /하는 .m

@property(nonatomic,copy) NSString *stringFromLayerB; 

bLayer를 제거 할 때 bButtonClicked :이 작업을 수행 할 수있는 다른 방법이 있습니다

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

. 콜백 메커니즘을 구현하고, 알림을 사용하고, 대리인 프로토콜 바인딩 일종의 BLayer 및 ALayer를 구현할 수 있습니다. 모두 귀하의 실제 (언급하지 않은) 요구 사항에 따라 다릅니다.

0

시나리오를 고려할 때 사용하는 것이 더 좋습니다 NSNotificationCenter. 단추를 두드렸을 때 블레이 어에게 알림을 게시하고 관찰자를 추가하여 정확하게 원하는대로 응답 할 수 있습니다.

앨리어스의 init에서 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"BlayerNotification" object:nil];을 추가하고 dealloc에서 [[NSNotificationCenter defaultCenter] removeObserver:self];을 추가하십시오.

같은 그것의 선택 : 이제

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

했나 버튼을 클릭 할 때 blayer에서 알림을 게시 할 수 있습니다 :

-(void)bbuttonclicked:(id)sender { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BlayerNotification" object:self]; 
    [self removeFromParentAndCleanup:YES]; 
}