在视图之间切换后,Swift Timer()将不会更新标签 [英] Swift Timer() will not update label after switching between views

查看:102
本文介绍了在视图之间切换后,Swift Timer()将不会更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题是我的用户在计时器仍在运行时在这两个视图之间来回切换-当在视图之间切换时,计时器仍会像往常一样递增,但是一旦来回切换它们将停止更新其各自的标签

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.

计时器的实现方式如下:

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")
}

计时器无效按钮的实现方式为:

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)
}

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

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.

不幸的是,我对Swift并不是很精通,而且有点茫然-对更好的实现的任何帮助甚至想法都将不胜感激.谢谢!

推荐答案

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

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.

您应该使用 unwind segue 从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.

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

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.

这篇关于在视图之间切换后,Swift Timer()将不会更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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