2013-03-26 1 views
1

Obj-c를 처음 사용했습니다. 나는 그것이 성공하면 var 부울을 YES로 설정하는 클래스를 가지고있다. (게임 센터 로그인 = 성공), 할 일이 무엇인지, 예를 들어 YES 일 때 청취하는 var에 청취자가있다. 일부 코드. 내가 블록을 사용합니까? Sparrow 프레임 워크도 사용하고 있습니다.Objective C에서 Block을 사용하여 BOOL이 설정되었는지 확인하십시오.

여기 내 GameCenter.m 파일

-(void) setup 
{ 
    gameCenterAuthenticationComplete = NO; 

    if (!isGameCenterAPIAvailable()) { 
     // Game Center is not available. 
     NSLog(@"Game Center is not available."); 
    } else { 
     NSLog(@"Game Center is available."); 

      __weak typeof(self) weakSelf = self; // removes retain cycle error 

      GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; // localPlayer is the public GKLocalPlayer 

     __weak GKLocalPlayer *weakPlayer = localPlayer; // removes retain cycle error 

      weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) 
      { 
       if (viewController != nil) 
       { 
        [weakSelf showAuthenticationDialogWhenReasonable:viewController]; 
       } 
       else if (weakPlayer.isAuthenticated) 
       { 
        [weakSelf authenticatedPlayer:weakPlayer]; 
       } 
       else 
       { 
        [weakSelf disableGameCenter]; 
       } 
      }; 
     } 

    } 

    -(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller 
    { 
     [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil]; 
    } 

    -(void)authenticatedPlayer:(GKLocalPlayer *)player 
    { 
     NSLog(@"%@,%@,%@",player.playerID,player.displayName, player.alias); 

     gameCenterAuthenticationComplete = YES; 

    } 

    -(void)disableGameCenter 
    { 

    } 

에 내 코드입니다하지만 그 gameCenterAuthenticationComplete가 YES 동일한 경우 다른 개체에서 알 필요가있다.

답변

3

대리인 패턴을 사용할 수 있습니다. KVO 나 로컬 알림보다 사용하기가 훨씬 쉬우 며 Obj-C에서 많이 사용됩니다.

알림은 특정 상황에서만 사용해야합니다 (예 : 듣고 싶은 사람이나 듣기가 1 명 이상인 사람을 모르는 경우).

블록은 여기에서 작동하지만 대리인은 똑같습니다.

2

개체의 속성을 볼 때 KVO (Key-Value Observing)를 사용할 수 있지만 사례에 NSNotification을 게시하고 싶습니다.

Game Center 로그인이 일어 났을 때 알기를 원하는 대상이 있어야합니다. NSNotificationCenter에 등록한 다음 NSNotification을 Game Center 처리기에 게시하십시오. 자세한 내용은 알림 프로그래밍 주제을 읽어보십시오!

0

단일 대리자 개체에서 실행할 단일 메서드가있는 경우 해당 개체를 setter에서 호출 할 수 있습니다. 날이 속성에 이름을 부여하자

@property(nonatomic,assign, getter=isLogged) BOOL logged; 

당신이 세터를 구현하는 것이 충분 :

- (void) setLogged: (BOOL) logged 
{ 
    _logged=logged; 
    if(logged) 
     [_delegate someMethod]; 
} 

또 다른 (제안) 방법은 NSNotificationCenter를 사용하는 것입니다. NSNotificationCenter를 사용하면 여러 객체에 알릴 수 있습니다.

NSNotificationCenter* center=[NSNotificationCenter defaultCenter]; 
[center addObserver: self selector: @selector(handleEvent:) name: @"Logged" object: nil]; 

의 handleEvent : 선택기 YES 변경 기록마다 실행될 재산권 YES 변경 될 때 방법을 수행하고자하는 모든 객체에 등록해야한다. 속성이 변경 될 때마다 알림을 게시하십시오.

- (void) setLogged: (BOOL) logged 
{ 
    _logged=logged; 
    if(logged) 
    { 
     NSNotificationCenter* center=[NSNotificationCenter defaultCenter]; 
     [center postNotificationName: @"Logged" object: self]; 
    } 
} 
관련 문제