具有CompletionHandler的外部数据无法正常工作

时间:2010-09-09 11:55:31

标签: iphone objective-c xcode ios4

我一直在研究一些代码,我想弄清楚如何使用GameCenter GameKit解决这个问题。

有一些示例代码(现在向公众开放,不再预发布,所以我可以谈论它。)

@synthesize playerStorage;

- (void)loadPlayerData:(NSArray *)identifiers
{
    [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
        if (error != nil
        {
            // Handle the error.
        }
        if (players != nil)
        {
            // Process the array of GKPlayer objects.
            // If I put this array in one I have created for the ViewController
            // it doesn't actually store it. It's like it can't find it.
            // But it doesn't error.
            [self setPlayerStorage:players];

            // Then animate to show why I need to do it this way, I cannot pass data
            [UIView beginAnimations:@"fadeInPlayer" context:self];
            [UIView setAnimationDuration:1.0];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(fadeInFriend:finished:context:)];
            labelPlayer.alpha = 0.0;
            [UIView commitAnimations];
        }
     }];
}
- (void)fadeInFriend:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    // Try and get the contents of playerStorage and update the label
    labelPlayer.text = [[playerStorage objectAtIndex:0] alias];

    // Fade in player label but label is blank
    // This is all example data, I'm doing it differently in the actual app
}

正如您在上面所看到的,有一个“完成处理程序”,基本上,我希望能够从完成处理程序中获取数据并在运行某些动画时存储它。例如,淡出当前数据,然后淡入从完成处理程序返回的新数据。但它不允许我。

我尝试将信息存储在NSArray(实际代码中的NSDictionary)中,但是当我从另一个函数调用键时,信息是空白的。

任何想法如何绕过这个?

1 个答案:

答案 0 :(得分:0)

我做了类似的事情,但我只是将已返回的“玩家”中的元素(例如,别名)写入我的阵列,工作得很好 - 我想你可能只是将玩家的引用传递给你的阵列,所以你是只是看到“球员”。我的代码(已经从loadScoresWithCompletionHandler创建了“highScores”) -

for (int i = 0; i < [players count]; i ++)
                      {
                          NSMutableDictionary *thisEntry = [highScores objectAtIndex:i];
                          [thisEntry setObject:[[players objectAtIndex:i] alias] forKey:@"alias"];
                      }

无论如何,为我工作,如果你想要整块代码让我知道。