gpt4 book ai didi

ios - 尝试在其 View 不在窗口层次结构中的 UIViewController 上呈现 GKGameCenterViewController

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:47 24 4
gpt4 key购买 nike

当用户按下我的 SecondViewController 上的按钮时,我无法显示 Game Center 排行榜(带有得分/最高得分的游戏结束屏幕)。 UIbutton 是 ShowLeaderboard,您将在下面看到它。

我得到的错误是:

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

我曾尝试先关闭该 View ,但无论我做什么,我都无法只显示排行榜 View 。下面是我的 SecondViewController 代码:

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)
}
}

编辑 成功了!我所要做的就是改变

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

var vc = self

最佳答案

您可能会看到此警告,因为您在 dismissViewControllerAnimated 完成动画之前显示排行榜。您应该将 showLeader() 放在 dismissViewControllerAnimated 的完成参数中。

关于ios - 尝试在其 View 不在窗口层次结构中的 UIViewController 上呈现 GKGameCenterViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28719658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com