2

我正在尝试将 Apple 的游戏中心集成到我的应用程序中。我可以成功地将分数发布到排行榜,并显示排行榜,但是当我尝试关闭排行榜模式视图时,问题就来了。我从游戏工具包编程指南([url]http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html[/url])中遵循了苹果的代码方向.

我的游戏中心代码如下:

-(BOOL)isGameCenterAvailable{
    // Check for presence of GKLocalPlayer class.
    BOOL localPlayerClassAvailable = (NSClassFromString(@"GKLocalPlayer")) != nil;

    // The device must be running iOS 4.1 or later.
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
    return (localPlayerClassAvailable && osVersionSupported);
}

- (void) authenticateLocalPlayer
{
    if([self isGameCenterAvailable]){
        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
        [localPlayer authenticateWithCompletionHandler:^(NSError *error) {
            if (localPlayer.isAuthenticated)
            {
                // Perform additional tasks for the authenticated player.
            }
        }];
    }
}

- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
    scoreReporter.value = score;

    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
        if (error != nil)
        {
            // handle the reporting error
        }
    }];
}

- (void) showLeaderboard
{
    GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
    if (leaderboardController != nil)
    {
        leaderboardController.leaderboardDelegate = self;
        [self presentModalViewController: leaderboardController animated: YES];
    }
    //[leaderboardController release];
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{

    if([self modalViewController] != nil){
        [self dismissModalViewControllerAnimated:YES];
    }

}

-(IBAction)show{
    [self showLeaderboard];
}

-(IBAction)submit{
    [self reportScore:9 forCategory:kLeaderboardID];
}

Xcode 告诉我问题行是 [self dismissModalViewControllerAnimated:YES]; 它说我的访问权限不正确,我知道这意味着我正在尝试访问一个错误的指针,但我不明白为什么任何东西都不会无效。自我报告说它有一个 modalviewcontroller。我尝试了各种变体,但我完全困惑为什么它会给我错误。

任何帮助或建议将不胜感激。

提前致谢!

4

1 回答 1

0

我刚刚在我的应用程序上遇到了一个非常相似的问题。我发现它与 ModalViewController 本身无关,而是与显示它的视图控制器相关。

如果您使用 Profiler 中的僵尸选项来分析应用程序,您将能够看到正在发布的某些内容不应该发布(很可能是 UIImage 或 UIView)。您应该能够追踪分配僵尸对象的函数,以找到导致问题的真实对象。

我假设当 ModalViewController 被解除时显示错误的原因是在对话框消失后调用了各种视图元素以重绘或刷新,然后访问了一些不应该被释放的东西。

希望这可以帮助。

于 2011-08-02T18:17:00.650 回答