1

当一个人接受邀请时,gameKit 会调用以下委托方法

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
    NSLog(@"%@ accepted invite",player.playerID);

    RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    [root popToRootViewControllerAnimated:YES];
    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithInvite:invite];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}

当用户尚未呈现GKMatchmakerViewController.

如果邀请进入时用户已经在使用 GKMatchmakerViewController(例如:当用户已经在尝试查找匹配项时),这将不起作用。如果是这种情况,当邀请进入时,我会收到以下警告,并且不会显示带有邀请的控制器(因为已经出现了 matchmakercontroller)。结果,用户无法响应邀请。(什么都没发生)。

Warning: Attempt to present <GKMatchmakerViewController: 0x1458a250>  on <RIYGameNavigationController: 0x1461f190> which is already presenting <GKMatchmakerViewController: 0x1465fcb0>

在 iOS6 中,可以通过以下方式解决此问题:

 if([topLevelViewController modalViewController] != nil)      
[topLevelViewController dismissModalViewControllerAnimated:NO];

然后创建GKMatchmakerViewController. dismissModalViewControllerAnimated:在 iOS6 中已被弃用。建议dissmissViewControllerAnimated:completion:改用,但对我不起作用。这是我尝试过的:

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
    NSLog(@"%@ accepted invite",player.playerID);

    RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    [root popToRootViewControllerAnimated:YES];
    if ([[root presentedViewController]class] == [GKMatchmakerViewController class]) {
        [root dismissViewControllerAnimated:YES completion:nil];
    }

    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithInvite:invite];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}

if 块被输入,但现有的 GKMatchmakerViewController 没有被解除,新的也没有出现。如果我在 if 语句中放置一个断点,当我在中断后“继续”时,视图控制器被解除,但新的控制器不会弹出。

我怎样才能得到这个工作?

4

1 回答 1

0

我通过在完成块中呈现视图来解决这个问题。

- (void)presentMatchmakerViewControllerWithInvite:(GKInvite *)invite
{
    RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithInvite:invite];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
     NSLog(@"%@ accepted invite",player.playerID);
     RIYGameNavigationController *root = (RIYGameNavigationController*)[[[UIApplication sharedApplication]delegate]window].rootViewController;
    [root popToRootViewControllerAnimated:YES];
    if ([[root presentedViewController]class] == [GKMatchmakerViewController class]) {
        [root dismissViewControllerAnimated:YES completion:^{
            [self presentMatchmakerViewControllerWithInvite:invite];
        }];
    }
    else
    {
        [self presentMatchmakerViewControllerWithInvite:invite];
    }
}

它似乎有效,但我觉得它很笨拙。如果有人有更好的答案,我会将其标记为正确的

于 2013-11-20T13:14:27.970 回答