开发者

Dismissing GameKit modal view

I am trying to integrate Apple's game center into my application. I can successfully post scores to the leader board, and show the leader board, but the problem comes when I try to dismiss the leader board modal view. I've followed apple's code direction from the Game Kit Programming Guide ([url]http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html[/url]).

My code is as follows for Game Center:

-(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 tells me the problem line is [self dismissModalViewControllerAnimated:YES]; it says I'm getting bad access, which I know means I'm trying to access a bad pointer, but I don't see why anything wouldn't be invalid. Self reports that it has a modalviewcontroller. I've tried all sorts of variants, an开发者_如何学JAVAd I'm completely baffled as to why it is giving me errors.

Any help or suggestions would be greatly appreciated.

Thanks in advance!


I just had a very similar issue on my App. I discovered that it was not related to the ModalViewController itself but the view controller displaying it.

If you profile the app using the zombies option in the Profiler, you will be able to see that something is being released that should not be (Most likely a UIImage or UIView). You should be able to track down the function where the zombied object was allocated to find the real object causing the trouble.

I am supposing that the reason the error shows when the ModalViewController is dissmissed is that various view elements are called to redraw or refresh after the dialog goes away and then something gets accessed that was released when it shouldn't have been.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜