简体   繁体   English

初始化GKMatchMakerViewController时出错

[英]Error initializing GKMatchMakerViewController

I am trying to make a simple online multiplayer game which uses Real-Time Matches. 我正在尝试制作一个使用Real-Time Matches的简单在线多人游戏。 However, when I try to initiate the GKMatchmakerViewController it throws an error. 但是,当我尝试启动GKMatchmakerViewController它会抛出一个错误。
Here is the current code I am running: 这是我正在运行的当前代码:

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)
}

However it throws this error when run: 但是,它在运行时抛出此错误:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller <GameName.GameViewController: 0x12e61a930> as a child of view controller:<GKMatchmakerViewController: 0x12e92b800>'

Now I have had this error previously when attempting to use GameCenter leaderboards. 现在我在尝试使用GameCenter排行榜时遇到了此错误。 I fixed it by initiating the leaderboard view controller without the rootViewController parameter. 我通过在没有rootViewController参数的情况下启动排行榜视图控制器来修复它。 However, that is not an option for GKMatchmakerViewController and it will simply throw a different error. 但是,这不是GKMatchmakerViewController的选项,它只会引发不同的错误。 Any help is greatly appreciated thanks! 非常感谢任何帮助!

EDIT: I know a lot of you have focused on the line where I present the view controller, however I think the error but be when I first initialize the view controller: var gcViewController: GKMatchmakerViewController = GKMatchmakerViewController(rootViewController: self) and might have something to do with setting the rootViewController to self. 编辑:我知道很多你专注于我提供视图控制器的行,但我认为错误,但是当我第一次初始化视图控制器时: var gcViewController: GKMatchmakerViewController = GKMatchmakerViewController(rootViewController: self) ,可能有一些东西将rootViewController设置为self。 That's just a thought though. 这只是一个想法。 Thanks again! 再次感谢!

Okay, I have gone back and rewritten the code as follows: 好的,我已经回去并重写代码如下:

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)
    }
}

So, if anyone in the future has trouble making a GKMatchmakerViewController this is how. 所以,如果将来有人在制作GKMatchmakerViewController时遇到问题,那就是这样的。 I just needed to use a matchRequest rather than using the rootViewController. 我只需要使用matchRequest而不是使用rootViewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM