由于尝试将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
我做错了什么?任何帮助表示赞赏!
答案 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