如果已经呈现GKMatchmakerViewController并在收到邀请时提供新实例,如何解散?

时间:2013-11-20 12:59:24

标签: ios objective-c uiviewcontroller gamekit gkmatchmaker

当一个人接受邀请时,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(例如:用户已经尝试查找匹配项)时,这不起作用。如果是这种情况,当邀请进入时,我收到以下警告,并且没有呈现带有邀请的控制器(因为已经有一个matchmaker控制器被呈现)。结果,用户无法响应邀请。 (没有任何事情发生)。

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

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

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

然后创建GKMatchmakerViewController。 iOS6中已弃用dismissModalViewControllerAnimated:。建议使用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不会被解除,并且新的GKMatchmakerViewController不会显示。如果我在if语句中设置了一个断点,当我在休息后“继续”时,视图控制器将被解除,但新的控制器不会弹出。

我怎样才能使这个工作?

1 个答案:

答案 0 :(得分: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];
    }
}

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