Phil
Phil

Reputation: 3043

Use Block in Objective C to find out if a BOOL has been set?

I'm new to Obj-c. I've got a class which sets a var boolean to YES if it's successful (Game Center login = successful), what it would be great to do, is somehow have a listener to that var that listens to when it is YES and then executes some code. Do I use a block for that? I'm also using the Sparrow framework.

Here's my code in my GameCenter.m file

-(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
    {

    }

But I need to know from a different object if that gameCenterAuthenticationComplete equals YES.

Upvotes: 1

Views: 179

Answers (3)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

If there is a single method to execute on a single delegate object, you can simply call it in the setter. Let me give a name to this property:

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

It's enough that you implement the setter:

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

Another (suggested) way is to use NSNotificationCenter. With NSNotificationCenter you can notify multiple objects. All objects that want to execute a method when the property is changes to YES have to register:

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

The handleEvent: selector will be executed every time that logged changes to YES. So post a notification whenever the property changes:

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

Upvotes: 0

Sulthan
Sulthan

Reputation: 130102

You can use a delegate pattern. It's far easier to use than KVO or local notifications and it's used a lot in Obj-C.

Notifications should be used only in specific situations (e.g. when you don't know who wants to listen or when there are more than 1 listeners).

A block would work here but the delegate does exactly the same.

Upvotes: 3

Taum
Taum

Reputation: 2551

You could use KVO (Key-Value Observing) to watch a property of your object, but I'd rather post a NSNotification in your case.

You'll need to have the objects interested in knowing when Game Center login happened register themselves to NSNotificationCenter, then post the NSNotification in your Game Center handler. Read the Notification Programming Topics for more details !

Upvotes: 2

Related Questions