簡體   English   中英

初始化GKMatchMakerViewController時出錯

[英]Error initializing GKMatchMakerViewController

我正在嘗試制作一個使用Real-Time Matches的簡單在線多人游戲。 但是,當我嘗試啟動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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM