不知道为什么我的loadmatcheswithmatchdata不起作用

时间:2013-03-14 21:52:04

标签: ios objective-c game-center gamekit

我正在创建一个公共方法来从我的gamecenterhelper.m

返回我的所有匹配项

我有这个:

+(NSArray *)retrieveMatchesWithMatchData {
    __block NSArray *myMatches = nil;
    [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {
        if (error) {
            NSLog(@"There was an error loading matches");
        }
        else {
            myMatches = matches;
        }
    }];
    return myMatches;
}

但是当我调用它时返回null,即使我有活动匹配。 call方法如下所示:

NSLog(@"My matches: %@",[GameCenterHelper retrieveMatchesWithMatchData]);

谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

这就是街区的性质。您的块是异步执行的。您无法同步加载Game Center匹配项。让我们把它变成一个实例方法:

-(void)retrieveMatchesWithMatchData {
    [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {
        if (error) {
            NSLog(@"There was an error loading matches");
        }
        else {
            [self matchesLoaded:matches];
        }
    }];
}

然后用这个方法处理匹配:

-(void)matchesLoaded:(NSArray *)matches {
    //do something with your matches
}

编辑:

有一种简单的方法可以做你想要的。您可以使用Apples标准视图控制器来显示当前匹配项:​​

GKMatchRequest *request = [[GKMatchRequest alloc] init]; 
request.minPlayers = minPlayers;     
request.maxPlayers = maxPlayers;

GKTurnBasedMatchmakerViewController *mmvc = 
[[GKTurnBasedMatchmakerViewController alloc] 
 initWithMatchRequest:request];
mmvc.turnBasedMatchmakerDelegate = self;
mmvc.showExistingMatches = YES;
[self presentViewController:mmvc animated:NO completion:nil];