如果GKMatchmakerViewController已经被呈现,如何在收到邀请时关闭它并呈现新实例



当某人接受邀请时,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,则此操作不起作用(例如:当用户已经在尝试查找匹配项时)。如果是这种情况,当邀请进来时,我会收到以下警告,并且带有邀请的控制器不会出现(因为已经出现了一个mathercontroller)。因此,用户无法响应邀请。(什么都没发生)。

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

在iOS6中,可以通过以下操作来绕过此问题:

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

然后创建CCD_ 2。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不会被忽略,新的也不会出现。如果我在If语句中放置了一个断点,当我在断点后"继续"时,视图控制器将被解雇,但新的控制器不会弹出。

我该怎么做?

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

- (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];
    }
}

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

最新更新