在视图之间切换后,Swift Timer()不会更新标签

时间:2020-12-22 02:48:35

I have a really basic two-view timer app I'm working on, wherein on View1 I have 30 buttons (15 buttons start timers, 15 of them invalidate each respective timer) and on View2 I have some other functionality not pertinent to my issue.

我有一个非常基本的双视图计时器应用程序我正在研究,其中在View1上我有30个按钮(15个按钮启动计时器,其中15个使每个相应的计时器无效)和View2我有一些其他功能与我的问题无关。

The issue is that my user switches back and forth between these two views while the timers are still running - the timers will still increment as normal when switching between the views but will cease updating their respective labels once they are switched back and forth.

问题是我的用户在定时器仍在运行时在这两个视图之间来回切换 - 定时器在视图之间切换时仍然会正常增加,但是一旦它们来回切换就会停止更新各自的标签。

The timers are implemented as such:

定时器实现如下:

switch timedBehavior {
        case "Introduction":
            timer1 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)

        case "Observing Stationary":
            timer2 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action2), userInfo: nil, repeats: true)

        case "Observing Moving":
            timer3 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action3), userInfo: nil, repeats: true)

default:
            print("Error in Timer Button Func")
}

The timer invalidating buttons are implemented as:

计时器无效按钮实现为:

switch stopButtons {

        case "stpBtn1":
            timer1.invalidate()
        case "stpBtn2":
            timer2.invalidate()
        case "stpBtn3":
            timer3.invalidate()
default:
        print("Error in Stop Button Func")

}

And each timer performs this functionality: (increments a number and updates a label)

每个计时器都执行此功能:(递增数字并更新标签)

func action()
{
    totalTime1 += 1
    timeLabel1.text = String(totalTime1)
}

So far I have tried to invalidate and immediately restart a particular timer if it was running in viewDidLoad() - which actually seemingly created two timers and doubled the speed of my increments.

到目前为止,我已经尝试使无效并立即重新启动特定的计时器,如果它在viewDidLoad()中运行 - 它实际上似乎创建了两个计时器并使我的增量速度加倍。

I'm not very well versed with Swift unfortunately and am at a bit of a loss -- any help or even ideas on better implementations would be really appreciated. Thanks!

不幸的是,我不太熟悉Swift并且有点亏本 - 任何有关更好实现的帮助甚至想法都会非常感激。谢谢!

1 个解决方案

#1


1  

You are using segues to transition between VC1 and VC2. When you return from VC2, you are creating an entirely new VC1 which is why you don't see your labels updating.

您正在使用segue在VC1和VC2之间进行转换。当您从VC2返回时,您正在创建一个全新的VC1,这就是您没有看到标签更新的原因。

You should use an unwind segue to return to VC1 from VC2. See here for how to setup and use an unwind segue.

您应该使用展开segue从VC2返回VC1。请参阅此处了解如何设置和使用展开segue。

Since you are using swipe gestures to transition between views, you will need to call the unwind segue programmatically. See the second half of this answer for how to set up the unwind segue and give it an identifier so that you can call it with performSegue(withIdentifier:sender:) from your swipe handler function.

由于您使用滑动手势在视图之间进行切换,因此您需要以编程方式调用unwind segue。请参阅本答案的后半部分,了解如何设置展开segue并为其指定一个标识符,以便您可以从滑动处理程序函数中使用performSegue(withIdentifier:sender :)调用它。

#1


1  

You are using segues to transition between VC1 and VC2. When you return from VC2, you are creating an entirely new VC1 which is why you don't see your labels updating.

您正在使用segue在VC1和VC2之间进行转换。当您从VC2返回时,您正在创建一个全新的VC1,这就是您没有看到标签更新的原因。

You should use an unwind segue to return to VC1 from VC2. See here for how to setup and use an unwind segue.

您应该使用展开segue从VC2返回VC1。请参阅此处了解如何设置和使用展开segue。

Since you are using swipe gestures to transition between views, you will need to call the unwind segue programmatically. See the second half of this answer for how to set up the unwind segue and give it an identifier so that you can call it with performSegue(withIdentifier:sender:) from your swipe handler function.

由于您使用滑动手势在视图之间进行切换,因此您需要以编程方式调用unwind segue。请参阅本答案的后半部分,了解如何设置展开segue并为其指定一个标识符,以便您可以从滑动处理程序函数中使用performSegue(withIdentifier:sender :)调用它。