CompletionHandler的外部数据无法使用
我一直在研究一些代码,我想解决如何用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)中,但是当我从另一个函数调用密钥时,信息为空。
任何想法如何绕过这个?
我做了类似的事情,但是我只是将返回的“玩家”的元素(例如别名)写到我的数组中,工作得很好 - 我认为你可能只传递给玩家的引用给你的数组,所以你只是看到“玩家”。 我的代码(使用loadScoresWithCompletionHandler创建的“highScores”) -
for (int i = 0; i < [players count]; i ++)
{
NSMutableDictionary *thisEntry = [highScores objectAtIndex:i];
[thisEntry setObject:[[players objectAtIndex:i] alias] forKey:@"alias"];
}
无论如何,为我工作,如果你想让整个代码块让我知道。
链接地址: http://www.djcxy.com/p/51789.html