4

I am having troubles showing the Game Center leaderboard when the user presses a button on my SecondViewController (game over screen with score/top score). The UIbutton is ShowLeaderboard which you'll see below.

The error I get is:

Warning: Attempt to present <GKGameCenterViewController: 0x7fb1c88044a0> on <UIViewController: 0x7fb1c2624e90> whose view is not in the window hierarchy!

I have tried dismissing the view first but no matter what I do I can't just get the leaderboard view to show. Below is my SecondViewController code:

import UIKit
import GameKit

class SecondViewController: UIViewController, GKGameCenterControllerDelegate {


@IBOutlet var scoreLabel: UILabel!
@IBOutlet var HighScoreLabel: UILabel!
var receivedString: String = ""
var receivedHighScore: String = ""


override func viewDidLoad() {
    super.viewDidLoad()
    scoreLabel.text = receivedString
    HighScoreLabel.text = receivedHighScore

}


@IBAction func ShowLeaderboard(sender: UIButton) {
    dismissViewControllerAnimated(true, completion:nil)
    showLeader()
}


func showLeader() {
    var vc = self.view?.window?.rootViewController
    var gc = GKGameCenterViewController()
    gc.gameCenterDelegate = self
    vc?.presentViewController(gc, animated: true, completion: nil)
}



func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}






override func prefersStatusBarHidden() -> Bool {
    return true
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


@IBAction func retryButton(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}
}

EDIT Got it working! All I had to do was change

var vc = self.view?.window?.rootViewController

to

var vc = self
DanTdd
  • 322
  • 3
  • 10

2 Answers2

0

You are probably seeing this warning because you are displaying the Leaderboard before dismissViewControllerAnimated has finished the animation. You should place the showLeader() inside the completion argument of dismissViewControllerAnimated.

Aris
  • 1,411
  • 9
  • 14
  • Hmmm even when I do ` gameCenterViewController.dismissViewControllerAnimated(true, completion: {self.showLeader()})` it still gives me the same error – DanTdd Feb 25 '15 at 13:55
  • are you dismissing SecondViewController and then trying to present the Leaderboard? – Aris Feb 25 '15 at 15:04
  • I tried that with the code above unless i did it wrong? – DanTdd Feb 25 '15 at 15:20
  • You should present the leaderboard from the controller that presented SecondViewController. – Aris Feb 25 '15 at 15:29
0
Here is my Code. Hope it helps! 

if (self.levelGameAttemptCount == 3)
                    {
                        self.canRestart = false

                        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let vc = mainStoryboard.instantiateViewControllerWithIdentifier("gameOverControllerID") as! GameOverController
                        self.view!.window!.rootViewController!.dismissViewControllerAnimated(false, completion: nil)
                        UIApplication.sharedApplication().keyWindow!.rootViewController!.presentViewController(vc, animated: true, completion: nil)
                    }
                    else
                    {
                        self.canRestart = true
                    }
A.G
  • 13,048
  • 84
  • 61