i coding in swift , getting following error message submit button. leaderboard not show in simulator, gives me error message below
<gkgamecenterviewcontroller: 0x7a8d0800> on <chinese_quiz.openingviewcontroller: 0x78688aa0> view not in window hierarchy!
below game centre code app.
override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. randomize() hide() authenticatelocalplayer() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func submit(sender: anyobject) { savehighscore(score) showleaderboard() } func authenticatelocalplayer(){ var localplayer = gklocalplayer.localplayer() localplayer.authenticatehandler = {(viewcontroller, error) -> void in if (viewcontroller != nil) { self.presentviewcontroller(viewcontroller, animated: true, completion: nil) } else { println((gklocalplayer.localplayer().authenticated)) } } } func gamecenterviewcontrollerdidfinish(gamecenterviewcontroller: gkgamecenterviewcontroller!) { gamecenterviewcontroller.dismissviewcontrolleranimated(true, completion: nil) } func showleaderboard() { var vc = self.view?.window?.rootviewcontroller var gc = gkgamecenterviewcontroller() gc.gamecenterdelegate = self vc?.presentviewcontroller(gc, animated: true, completion: nil) } func savehighscore(score:int) { //check if user signed in if gklocalplayer.localplayer().authenticated { var scorereporter = gkscore(leaderboardidentifier: "chineseweather") //leaderboard id here scorereporter.value = int64(score) //score variable here (same above) var scorearray: [gkscore] = [scorereporter] gkscore.reportscores(scorearray, withcompletionhandler: {(error : nserror!) -> void in if error != nil { println("error") } }) } }
solved turning this:
func showleaderboard() { var vc = self.view?.window?.rootviewcontroller var gc = gkgamecenterviewcontroller() gc.gamecenterdelegate = self vc?.presentviewcontroller(gc, animated: true, completion: nil) }
into this:
func showleaderboard() { var vc = self var gc = gkgamecenterviewcontroller() var gamecenterdelegate: gkgamecentercontrollerdelegate! // above text replaced gc.gamecenterdelegate = self vc.presentviewcontroller(gc, animated: true, completion: nil) }
i don't know why, worked.
Comments
Post a Comment