问题
I'm presenting a GKGameCenterViewController from an SKScene. This is my code:
-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard{
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
gcViewController.leaderboardIdentifier = _leaderboardIdentifier;
// _spriteViewController references an UIViewController
[_spriteViewController presentViewController:gcViewController animated:YES completion:nil];
}
I don't have problems presenting the window, however I cannot manage to let users close it. I implemented the method gameCenterViewControllerDidFinish
however the function is not called at all. What do I have to do so users can close the leaderboard?
回答1:
I fixed by adding the line gcViewController.gameCenterDelegate = _spriteViewController;
-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard{
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
gcViewController.leaderboardIdentifier = _leaderboardIdentifier;
gcViewController.gameCenterDelegate = _spriteViewController; // er
// _spriteViewController references an UIViewController
[_spriteViewController presentViewController:gcViewController animated:YES completion:nil];
}
While it worked I got the warning:
Assigning to 'id<GKGameCenterControllerDelegate>' from incompatible type 'UIViewController *__weak'
For anyone in this situation the problem was simply that _spriteViewController
was of typeUIViewController
, changing that to my custom class fixed the issue. You can also declare the property referencing the ViewController like this:
UIViewController <GKGameCenterControllerDelegate> *_spriteViewController;
来源:https://stackoverflow.com/questions/24688134/cannot-dismiss-gkgamecenterviewcontroller-presented-from-skscene