我已经阅读了几篇关于这个问题的帖子,但没有一篇能解决我的问题。
我正在编写一个应用程序,我必须单击一个按钮(“准备”)才能转到以下 ViewController。单击按钮时,它还会在两个 View Controller 之间传递数据。
问题是,当我单击该按钮时,以下 ViewController 会加载两次。因此,如果我想返回,我必须通过两个相同的 ViewController 返回。
我已经检查了 segue、类名和文件名,但没有任何修复。
我也创建了一个新项目并从头开始重写了所有代码,但最后还是不行。
但是,我注意到当我添加 prepare(forSegue:) 函数和 performSegue 函数时问题出现了。没有它,ViewController 只加载一次。但是,当然,如果没有它,我将无法在 View 之间传递数据......
下面是我的两个view的截图和两个函数的代码:
// 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 时,这是因为您有:
- Storyboard 上的 segue,直接从 Button 开始
- IBAction 附加到您调用相同 Segue 的 performSegue 的按钮
要解决此问题,您需要创建一个直接从 ViewController 启动的 Segue。在您可以使用 IBAction 和 performSegue 之后
关于ios - 相同的 View Controller 加载两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46018202/