GameCenter能够打开。但是,当在右上角按下“完成”按钮以解除GameCenter时,GameCenter仍然保持打开状态。如何关闭GameCenter?
以下是我的代码:
- (void) presentLeaderboards
{
GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.viewState = GKGameCenterViewControllerStateLeaderboards;
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: leaderboardController animated: YES completion:nil];
}
}
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)viewController
{
NSLog(@"Close");
UIViewController *vc = self.view.window.rootViewController;
[vc dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:1)
您永远不会为GKGameCenterViewController
设置委托,因此永远不会调用您的gameCenterViewControllerDidFinish:
方法。您应该通过一些调试自己找到它。
在dismissViewControllerAnimated
上致电viewController
。
您的代码应该更像:
- (void) presentLeaderboards
{
GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.gameCenterDelegate = self;
leaderboardController.viewState = GKGameCenterViewControllerStateLeaderboards;
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: leaderboardController animated: YES completion:nil];
}
}
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)viewController
{
NSLog(@"Close");
[viewController dismissViewControllerAnimated:YES completion:nil];
}
添加行:
leaderboardController.gameCenterDelegate = self;
可能会导致新的错误。如果是这样,您需要在@implementation
行之前将.m添加到.m。
@interface MyViewControllerNameHere () <GKGameCenterControllerDelegate>
@end
如果您已有类扩展名,只需添加GKGameCenterControllerDelegate
部分。