我无法关闭 GameCenter leaderboard/view

I am unable to dismiss a GameCenter leaderboard/view

GameCenter可以打开了。但是,当按下右上角的 "done" 按钮关闭 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];
}
  1. 您从未为您的 GKGameCenterViewController 设置委托,所以您的 gameCenterViewControllerDidFinish: 方法永远不会被调用。你应该通过一点调试自己找到这个。

  2. viewController 上致电 dismissViewControllerAnimated

您的代码应该更像:

- (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。

@interface MyViewControllerNameHere () <GKGameCenterControllerDelegate>

@end

如果您已经有了 class 扩展名,只需添加 GKGameCenterControllerDelegate 部分。