2014-10-01 79 views
0

在我的纸牌游戏游戏中,我尝试创建一个匹配项,对于某个部分来说,它可以工作;匹配成功创建游戏中心:匹配

- (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”?

谢谢!

回答

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]; 
+0

感谢您的回答!我明白你在说什么,但是我的代码和平是在一个'GameKitHelper'类中。调用self是不可能的,因为它会给出一个错误,说明'presentViewController:...'在'GameKitHelper'类中不是可见的接口。要清楚:这是一个自定义的'NSObject'类,而不是'UIViewController'类。 – Niels 2014-10-01 19:01:23

+0

@Niels啊,理解。你的AppDelegate类的名字是什么?只是AppDelegate? – Undo 2014-10-01 19:02:01

+0

是的,它是AppDelegate – Niels 2014-10-01 20:21:35