我读过关于这个问题的几篇文章,但没有一个能解决我的问题。
我正在编写一个应用程序,我必须单击一个按钮(“准备”)才能转到以下 ViewController。单击按钮时,它还会在两个视图控制器之间传递数据。
问题是,当我单击按钮时,以下 ViewController 会加载两次。因此,如果我想返回,我必须通过两个相同的 ViewController 返回。
我已经检查了segue、类名和文件名,但没有任何解决办法。
我还创建了一个新项目并从头开始重写了所有代码,但最终仍然不起作用。
但是,我注意到当我添加prepare(forSegue:)函数和performSegue函数时问题出现了。如果没有它,ViewController 只会加载一次。但是当然,如果没有它,我就无法在视图之间传递数据......
这是我的两个视图的屏幕截图和两个函数的代码:
第一视角
第二个视图
// Prepare the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "prepareSegue" {
let timerViewController = segue.destination as! CountdownViewController
timerViewController.timeInterval1 = waitingTime
timerViewController.timeInterval2 = shootingTime
timerViewController.lastSeconds = lastSeconds
timerViewController.currentTimeInterval = waitingTime
}
}
// Prepare the timer when the button is pushed
@IBAction func prepareTimer(_ sender: Any) {
performSegue(withIdentifier: "prepareSegue", sender: self)
}
可能,当出现两个 ViewController 时,这是因为您有:
- 故事板上的 Segue 直接从按钮开始
- IBAction 附加到您调用同一 Segue 的 PerformSegue 的按钮
要解决这个问题,您需要创建一个直接从 ViewController 启动的 Segue。之后就可以使用IBAction和performSegue了
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)