游戏中心排行榜 - 无法将GKLeaderboardViewController委托设置为自己(CCLayer)

时间:2015-04-23 20:23:03

标签: ios objective-c cocos2d-iphone game-center

由于尝试将GKLeaderboardViewController委托设置为调用实例而导致的错误,以下代码将无法编译。

错误消息是:

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

其中MainMenu属于CCLayer类型。

如果注释语句(leaderboard.delegate = self)被注释掉,代码将被编译,排行榜将显示,但是当按下“完成”按钮时将不会调用回调。

这是代码:

- (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];
    }
}

顺便说一下,这是我对象声明的标题:

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

#import <GameKit/GameKit.h>

@interface MainMenu : CCLayer <CCGameButtonDelegate, GKLeaderboardViewControllerDelegate> {

}

+ (CCScene *) scene;

@end

我做错了什么?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

我相信你要找的是leaderboardDelegate属性而不是delegate属性。 leaderboardDelegate需要<GKLeaderboardViewControllerDelegate>delegate需要<UINavigationControllerDelegate>,因此会出现错误消息。

检查申请leaderboardDelegate的苹果文档 https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboardViewController_Ref/#//apple_ref/occ/instp/GKLeaderboardViewController/leaderboardDelegate