2014-10-01 134 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