我正在尝试制作一个使用实时比赛的简单在线多人游戏。但是,当我尝试启动GKMatchmakerViewController
时会抛出错误,这是我正在运行的当前代码:
func openMatchmaker() {
var gcViewController: GKMatchmakerViewController = GKMatchmakerViewController(rootViewController: self)
gcViewController.matchmakerDelegate = self
gcViewController.hosted = false
gcViewController.matchRequest.minPlayers = 2
gcViewController.matchRequest.maxPlayers = 2
gcViewController.matchRequest.defaultNumberOfPlayers = 2
self.showViewController(gcViewController, sender: self)
self.navigationController?.pushViewController(gcViewController, animated: true)
}
但是,运行时会引发此错误:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller <GameName.GameViewController: 0x12e61a930> as a child of view controller:<GKMatchmakerViewController: 0x12e92b800>'
现在,我以前在尝试使用GameCenter排行榜时遇到此错误。我通过不使用
rootViewController
参数启动排行榜视图控制器来修复它。但是,这不是GKMatchmakerViewController
的选项,它只会引发另一个错误。任何帮助,万分感谢!编辑:我知道你们中的很多人都集中在介绍视图控制器的那一行,但是我认为错误在于我第一次初始化视图控制器时:
var gcViewController: GKMatchmakerViewController = GKMatchmakerViewController(rootViewController: self)
,可能与将rootViewController设置为self有关。不过,这只是一个想法。再次感谢! 最佳答案
好的,我回过头来重新编写了如下代码:
func displayGameMaker(){
if (GKLocalPlayer.localPlayer().authenticated){
var request = GKMatchRequest()
request.minPlayers = 2
request.maxPlayers = 2
request.inviteMessage = "You have been invited to a game!"
var vc = GKMatchmakerViewController(matchRequest: request)
vc.matchmakerDelegate = self
self.showViewController(vc, sender: self)
}
}
因此,如果将来有任何人在制作GKMatchmakerViewController时遇到问题,这就是方法。我只需要使用matchRequest而不是使用rootViewController。