Search code examples
uiviewcontrollergamekit

Dismissing GKGameCenterViewController


I'll demonstrate my problem like this: I've a project with 2 classes- ViewController and GCManage.

The ViewController is a subclass of UIViewController and GCManage of NSObject.

When I push a button in ViewController it calls this method:

-(IBAction)showLeaderboards:(id)sender{
    GCManage *manageGC = [[GCManage alloc] init];
    if (manageGC.isGCenabled == YES){
        GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
        if (gameCenterController != nil)
        {
            gameCenterController.gameCenterDelegate = manageGC;
            gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
            [self presentViewController:gameCenterController animated:YES completion:nil];
        }
    }
}

For this instance, assume that isGCenabled = YES.

Now GCManage interface is

@interface GCManage : NSObject <GKGameCenterControllerDelegate> (in h file, the first line).

I've implemented

- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
[gameCenterViewController.presentingViewController dismissViewControllerAnimated:YES completion:^(void){}];
[gameCenterViewController.presentedViewController dismissViewControllerAnimated:YES completion:^(void){}];
[gameCenterViewController dismissViewControllerAnimated:YES completion:^(void){}];
NSLog(@"Ran");
}

in GCManage but it doesn't appear to be called in any case.

Now when ViewController is the delegate of and implements

- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{

[gameCenterViewController.presentingViewController dismissViewControllerAnimated:YES completion:^(void){}];
NSLog(@"Ran");
}

it runs flawlessly. What's happening here?


Solution

  • Create a property in ViewController for your GCManage object. Your manageGC object created in showLeaderboards: is not being retained, and so the delegate function is never called.