TinoK
TinoK

Reputation: 311

Game Centre Leaderboard - Can't set GKLeaderboardViewController delegate to self (CCLayer)

The following code won't compile because of the error that results from trying to set the GKLeaderboardViewController delegate to the calling instance.

The error message is:

Assigning to 'id<UINavigationControllerDelegate>' from incompatible type 'MainMenu *'

where MainMenu is of type CCLayer.

If the assignment statement (leaderboard.delegate = self) is commented out, the code will compile, the leaderboard will display, but the callback will not be called when the "done" button is pressed.

This is the code:

- (void) showLeaderBoard {

    // Show GC leaderboard

    GKLeaderboardViewController *leaderboard = [[GKLeaderboardViewController alloc] init];

    if (leaderboard != nil) {

        leaderboard.delegate = self;
        leaderboard.category = @"ldrbrd_ref";

        AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
        [[app navController] presentViewController:leaderboard animated:YES completion:nil];
    }
}

Incidentally, this is my header for the object declaration:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCGameButton.h"

#import <GameKit/GameKit.h>

@interface MainMenu : CCLayer <CCGameButtonDelegate, GKLeaderboardViewControllerDelegate> {

}

+ (CCScene *) scene;

@end

What am I doing wrong? Any help appreciated!

Upvotes: 0

Views: 67

Answers (1)

tpatiern
tpatiern

Reputation: 415

I believe what you are looking for is the leaderboardDelegate property rather than the delegate property. leaderboardDelegate requires a <GKLeaderboardViewControllerDelegate> while delegate requires a <UINavigationControllerDelegate>, hence the error message.

Check the apple docs for leaderboardDelegate https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboardViewController_Ref/#//apple_ref/occ/instp/GKLeaderboardViewController/leaderboardDelegate

Upvotes: 1

Related Questions