当 GKGameCenterViewController 被关闭时如何显示特定的 UIViewController?
How to show a specific UIViewController when GKGameCenterViewController is dismissed?
我在继承自以下协议的 SKScene 中呈现 GKGameCenterViewController
。
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = GameViewController()
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
view?.window?.rootViewController?.present(vc, animated: true, completion: nil)
}
}
虽然 GKGameCenterViewController
显示完美,但当我尝试通过单击右上角的 X 关闭时,没有任何反应。我认为这是因为对我的原始 GameViewController
的引用已被释放。我怎样才能让这次解雇生效?
Your delegate should dismiss the Game Center view controller. If your game paused any gameplay or other activities, it can restart those services in this method.
这意味着您需要在委托中实现 gameCenterViewControllerDidFinish
方法并自行关闭 gameCenterViewController
。
你的 GameViewController()
中应该有这样的东西
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}
为了在 SKScene 上呈现 GKGameCenterViewController
,我需要找到当前显示的 UIViewController
引用并将其设置为委托。这是我使用的代码并且有效:
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
var currentViewController:UIViewController=UIApplication.shared.keyWindow!.rootViewController!
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = currentViewController as! GKGameCenterControllerDelegate
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
currentViewController.present(vc, animated: true, completion: nil)
}
}
我在继承自以下协议的 SKScene 中呈现 GKGameCenterViewController
。
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = GameViewController()
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
view?.window?.rootViewController?.present(vc, animated: true, completion: nil)
}
}
虽然 GKGameCenterViewController
显示完美,但当我尝试通过单击右上角的 X 关闭时,没有任何反应。我认为这是因为对我的原始 GameViewController
的引用已被释放。我怎样才能让这次解雇生效?
Your delegate should dismiss the Game Center view controller. If your game paused any gameplay or other activities, it can restart those services in this method.
这意味着您需要在委托中实现 gameCenterViewControllerDidFinish
方法并自行关闭 gameCenterViewController
。
你的 GameViewController()
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}
为了在 SKScene 上呈现 GKGameCenterViewController
,我需要找到当前显示的 UIViewController
引用并将其设置为委托。这是我使用的代码并且有效:
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
var currentViewController:UIViewController=UIApplication.shared.keyWindow!.rootViewController!
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = currentViewController as! GKGameCenterControllerDelegate
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
currentViewController.present(vc, animated: true, completion: nil)
}
}