我无法解雇GameCenter排行榜/视图

时间:2015-12-12 22:38:43

标签: ios objective-c game-center game-center-leaderboard

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

1 个答案:

答案 0 :(得分:1)

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

  2. dismissViewControllerAnimated上致电viewController

  3. 您的代码应该更像:

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