简体   繁体   中英

Swift - Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate'

I am trying to use GameCenter to show the leaderboards for the high scores in my game. I have been trying to use some code to implement GameCenter but it all fails.

I'm told to set gameCenterDelegate before the GameCenter view controller is shown.

I've been told to do it like this:

var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self

but this gives me the following error:

Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate'

I tried changing this:

class MenuViewController: UIViewController {

to:

class MenuViewController: GKGameCenterViewControllerDelegate {

but this gives me loads of errors, the first of which being on this line, reading:

Use of undeclared type 'GKGameCenterViewControllerDelegate'

I have been trying to get this to work for about a fortnight, and there seems to be no solution anywhere. Can someone please help me???

Your MenuViewController needs to be both a subclass of UIViewController and to declare that it implements the delegate. If you've imported GameKit correctly, then:

class MenuViewController: UIViewController, GKGameCenterControllerDelegate {
   ...
}

should work. I agree that the syntax is a bit confusing, with no demarkation between the superclass and the implemented protocols.

Edit: Whoops, simple error. The delegate is actually GKGameCenterControllerDelegate not GKGameCenterViewControllerDelegate . Declare that you implement it as above, then actually implement the methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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