1

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

1 回答 1

1
  1. 您永远不会为您设置委托,GKGameCenterViewController因此gameCenterViewControllerDidFinish:永远不会调用您的方法。您应该通过一些调试自己发现这一点。

  2. 打电话。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 中。

@interface MyViewControllerNameHere () <GKGameCenterControllerDelegate>

@end

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

于 2015-12-12T22:46:35.573 回答