游戏中心:对接

时间:2014-10-01 18:29:26

标签: ios uiviewcontroller

在我的纸牌游戏中,我正在尝试创建一个匹配,这对于某个部分起作用;

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {

self.match = match;
match.delegate = self;
if (!_matchStarted && match.expectedPlayerCount == 0) {
    NSLog(@"Ready to start match!");
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameVC"];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [viewController presentViewController:vc animated:YES completion:nil];
    //[viewController dismissViewControllerAnimated:YES completion:nil];
}
}

成功创建匹配后,我希望GKMatchmakerViewController被解雇,我希望显示“vc”UIViewController。在上面的示例中,这已经完成,但GKMatchmakerViewController不会被解雇。

如果我删除评论引号,它将在以某种方式被解雇后加载,如果我将该行放在presentViewController行之上,我会收到一条错误,指出我试图在视图控制器上呈现视图控制器那不在视图层次结构中。

如何解雇GKMVC并在“相同”时间显示“vc”?

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要关闭GKMatchMakerViewController,然后使用当前的视图控制器来呈现新模式:

if (!_matchStarted && match.expectedPlayerCount == 0) {
    NSLog(@"Ready to start match!");
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameVC"];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [viewController dismissViewControllerAnimated:YES completion:nil];
    [self presentViewController:vc animated:YES completion:nil];
}

编辑:获取当前根视图控制器的技巧是[[UIApplication sharedApplication] delegate].window.rootViewController。所以你应该能够用这个来表达你的模态:

[[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:vc animated:YES completion:nil];