我在使用iOS 6SDK的应用程序中集成游戏中心时遇到了问题。实际上,我使用的是Apple的示例代码,但它看起来并不完整:
我已经尝试过以下代码:
-(void) authenticateLocalPlayer {
GKLocalPlayer* localPlayer =
[GKLocalPlayer localPlayer];
localPlayer.authenticateHandler =
^(UIViewController *loginVC,
NSError *error) {
[self setLastError:error];
if ([GKLocalPlayer localPlayer].authenticated)
{
// authentication successful
[self enableGameCenterForPlayer:[GKLocalPlayer localPlayer]];
}
else if (loginVC)
{
// player not logged in yet, present the vc
[self pauseGame];
[self presentLoginVC:loginVC];
}
else
{
// authentication failed, provide graceful fallback
[self disableGameCenter];
}
};}
但问题是enableGameCenterForPlayer、pauseGame、presentLoginVC、disableGameCenter都不是实现的方法,它返回:
Instance method '-enableGameCenterForPlayer:' not found (return type defaults to 'id')
我如何解决这个问题?
谢谢
发布于 2013-01-31 03:29:50
我使用方法self presentLoginVC:VC来传递我的UITabViewController或UINavigationController viewController,因为下面的代码块不在主线程上。
localPlayer.authenticateHandler = ^(UIViewController *loginVC, NSError *error) {当你在一个块中时,你应该确保不要改变UI元素,因为你真的不知道它什么时候会完成,或者你会在你的应用程序中处于什么位置。可能有很多方法可以做到这一点,但这是我的解决方案。
下面是我的类别‘UITabBarController’.m文件(为没有子类化的类添加方法)我创建了方法presentLoginVC,并让它通过我的UITabBarController调用'showGameCenterViewController‘:
#import "UITabBarController+GameKitAdditions.h"
@implementation UITabBarController (GameKitAdditions)
-(void) showGameCenterViewController: (UIViewController *)VC {
[self presentViewController:VC animated:NO completion:nil];
}
-(void)dismissGameCenterViewController:(UIViewController *)VC {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end至于其他函数:
-(void) enableGameCenterForPlayer:(GKLocalPlayer *) localPlayer;
-(void) disableGameCenter;
-(void) pauseGame;它们可能很简单,只需将名为enableGameCenter的BOOL设置为YES或NO即可。要避免错误,您可以将这些原型添加到.h文件中,然后编写函数以将某些内容输出到NSLog()或其他内容。
https://stackoverflow.com/questions/13442425
复制相似问题