初始化GKMatchMakerViewController时出错

肯德尔

我正在尝试制作一个使用实时比赛的简单在线多人游戏。但是,当我尝试启动时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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章