同一视图控制器加载两次

Bed*_*olf 1 viewdidload viewcontroller ios segue swift

我已经阅读了有关此问题的几篇文章,但都没有解决我的问题。

我正在编写一个应用程序,我必须单击一个按钮(“准备”)以转到以下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)
}
Run Code Online (Sandbox Code Playgroud)

Giu*_*nza 6

可能当两个ViewController出现时是因为您有:

  1. 直接从Button开始的Storyboard上的segue
  2. IBAction附加到您调用相同Segue的performSegue的按钮上

要解决此问题,您需要创建一个直接从ViewController开始的Segue。之后可以使用IBAction和performSegue