同一视图控制器加载两次 [英] Same view controller loads twice

查看:64
本文介绍了同一视图控制器加载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've read several posts on this issue but none of them solved my problem.

我正在编写一个应用程序,其中我必须单击一个按钮(准备")以转到以下ViewController.单击该按钮时,它还会在两个视图控制器之间传递数据.

I'm coding an app where I have to click on a button ("Prepare") to go to the following ViewController. When the button is clicked, it also passes data between the two view controller.

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

The problem is, when I click the button, the following ViewController loads twice. Thus, if I want to go back I have to go back through two same ViewController.

我已经检查了segue,类名和文件名,但没有任何解决方法.
我还创建了一个新项目,并从一开始就重写了所有代码,但最后仍然无法正常工作.

I've checked the segue, the class names and files names but nothing fixes it.
I've also created a new project and rewritten all the code from the beginning but in the end, it still doesn't work.

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

However, I've noticed that the problem showed up when I added the prepare(forSegue:) function and the performSegue function. Without it the ViewController only loads once. But of course, I can't get the data passed between the views without it...

这是我的两个视图的屏幕快照和两个函数的代码:

Here is the screenshot of my two view and the code of the two functions :

第一视图
第二视图

//    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时,是因为您有:

Probably, when two ViewControllers appear it's because you have:

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

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

To fix this problem, you need to create a Segue which start directly from ViewController. After you can use the IBAction and the performSegue

这篇关于同一视图控制器加载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆