ios - 相同的 View Controller 加载两次

标签 ios swift segue viewcontroller viewdidload

我已经阅读了几篇关于这个问题的帖子,但没有一篇能解决我的问题。

我正在编写一个应用程序,我必须单击一个按钮(“准备”)才能转到以下 ViewController。单击按钮时,它还会在两个 View Controller 之间传递数据。

问题是,当我单击该按钮时,以下 ViewController 会加载两次。因此,如果我想返回,我必须通过两个相同的 ViewController 返回。

我已经检查了 segue、类名和文件名,但没有任何修复。
我也创建了一个新项目并从头开始重写了所有代码,但最后还是不行。

但是,我注意到当我添加 prepare(forSegue:) 函数和 performSegue 函数时问题出现了。没有它,ViewController 只加载一次。但是,当然,如果没有它,我将无法在 View 之间传递数据......

下面是我的两个view的截图和两个函数的代码:

First view
Second 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 时,这是因为您有:

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

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

关于ios - 相同的 View Controller 加载两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46018202/

相关文章:

ios - UI 不填充 iPhone 6 plus

iOS 碰撞检测

c# - WinForms 类似 iPad 的界面原型(prototype)设计应用程序

arrays - 如何在结构数组中找到最大/最小值

swift - 如何呈现带有文本字段的 UIAlertController,但该文本字段不能是第一响应者?

ios - 总是继续前进是可以接受的还是我应该最终从 UINavController 中弹出 View ?

ios - 如何为阿拉伯语希伯来语实现带 RTL 的 iOS?

ios - 连接 2 个字符串(或数组)

ios - Swift didSelectRowAt 将数组值传递给第二个 View

ios - 调用自定义 Segue 类执行方法,操作不执行任何操作