我通常会使用 segue 来切换 View Controller ,但我不认为我可以使用 segue 来完成我正在做的事情。当游戏结束时,我会出现一个警告,告诉用户游戏已经结束。用户可以在警报 View 上点击两个按钮。一个是“再试一次”,另一个是“主菜单”。当用户点击“主菜单”时,它会切换到主视图 Controller 。但是,当我使用“instantiateViewControllerWithIdentifier”时,每次用户点击“主菜单”时,它都会创建该主视图 Controller 的新实例。如何将警报 View 中的“主菜单”按钮切换到主视图 Controller 的原始实例?这是我的代码:
gameOverAlert.addAction(UIAlertAction(title: "Main Menu", style: .Default, handler: { (action: UIAlertAction!) in
self.ReturnToMainMenu()
}))
func ReturnToMainMenu(){
var storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Main = self.storyboard!.instantiateViewControllerWithIdentifier("Main") as! ViewController
self.presentViewController(Main, animated:true, completion:nil)
}
最佳答案
关闭您呈现的 View Controller (您的游戏),以便它显示您之前的 View Controller (您的主菜单)。
在您返回主菜单时,self.dismissViewControllerAnimated(false, completion: nil)
关于ios - 在 Swift 中显示以前创建的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32077943/