使用Swift设置游戏中心

时间:2015-09-09 05:46:00

标签: ios swift game-center

我在设置游戏中心排行榜时遇到了问题,因为我收到错误Cannot assign a value of type 'ViewController' to a value of type 'GKGameCenterControllerDelegate!'。我认为它与我放在顶部的代码有关:

class ViewController: UIViewController, GKGameCenterControllerDelegate, ADBannerViewDelegate {
//stuff
}

以及我在其中一个操作按钮中输入的代码:

//shows leaderboard screen
            func showLeader() {
                var vc = self.view?.window?.rootViewController
                var gc = GKGameCenterViewController()
                gc.gameCenterDelegate = self            //<------This is the problem child.
                vc?.presentViewController(gc, animated: true, completion: nil)
            }

发生了什么事?谢谢!

1 个答案:

答案 0 :(得分:3)

您收到此错误是因为ViewController不符合协议GKGameCenterControllerDelegate

要解决此问题,请将此GKGameCenterControllerDelegate的委托方法添加到ViewController类中:

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!){
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}

你的错误将会解决。