返回主视图控制器?
问题描述:
我是一个新的程序员,我有2个视图控制器,当我的游戏结束后,弹出一个“再次播放”按钮。返回主视图控制器?
而不是在第二个视图控制器上的计时器后重新启动游戏我希望该游戏再次返回到具有播放按钮的第一个视图控制器。我该怎么办?我有以下警报代码:
func subtractTime() {
seconds--
timerLabel.text = "Time: \(seconds)"
if(seconds == 0) {
timer.invalidate()
let alert = UIAlertController(title: "Time is up!",
message: "You scored \(count) points",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
action in self.setupGame()
}))
presentViewController(alert, animated: true, completion:nil)
}
}
答
它可以通过赛格瑞告诉故事板连接到另一个视图控制器在指定的动作称为
对于SEGUE被通常做,你将需要首先通过声明它的标识符来设置故事板中的segue。然后在你的功能,你可以presentViewController
let mainViewController = yourMainControllerName();
presentViewController(mainViewController, animated: true, completion: nil)
后立即拨打
performSegueWithIdentifier("segueId", sender: nil)
我相信你也能做到现在,你应该将其中一方在处理用户在警报解除后。 addAction
alert.addAction(action in self.setupGame() UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler:{(ViewController: UIViewController!) in let mainViewController: secondViewController = segue.mainViewController as secondViewController}