遇到错误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)
}
到底是怎么回事?谢谢!
最佳答案
由于您的ViewController
不符合协议GKGameCenterControllerDelegate
,因此出现此错误。
要解决此问题,请将GKGameCenterControllerDelegate
的委托方法添加到ViewController
类中:
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!){
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
您的错误将解决。