简体   繁体   English

swift3,我的计时器无法停止。 为什么?

[英]swift3,my timer can't stop. why?

this is i seting a timer function, the code like this: 这是我设置一个计时器函数,代码是这样的:

@IBAction func start(_ sender: UIButton) {

    Timer.scheduledTimer(timeInterval: 1,
                         target: self,
                         selector:#selector(ViewController.action),
                         userInfo: nil,
                         repeats: true)
}

@objc func action() {

    hoursMinutesSeconds()
    if stop == true{
        start = false
        timer.invalidate()
        timer.invalidate()
        time = 0 
    }
}

@IBAction func stop(_ sender: UIButton){

    start = false
    timer.invalidate()
    timer.invalidate()
    time = 0           
}

but, when i click the stop func, this function not work. 但是,当我单击停止功能时,此功能不起作用。 mean is the timer not stop. 意思是计时器没有停止。 timer stil working…… why? thank you for your time!! 计时器仍在工作……为什么?谢谢您的时间!

I think you didn't set timer value 我认为您没有设置timer

timer = Timer.scheduledTimer(timeInterval: 1,
                         target: self,
                         selector:#selector(ViewController.action),
                         userInfo: nil,
                         repeats: true)

Ensure that you are invalidating correct instance of Timer. 确保您使Timer的正确实例无效。 Like in start function , you didn't assign timer instance to any object, but in stop function ,you are using timer vaiable to stop that timer. 像在start function一样,您没有将计时器实例分配给任何对象,但是在stop function ,您使用的是可利用的timer来停止该计时器。 Which means you try to invalidate a variable, which never instantiated. 这意味着您尝试使从未实例化的变量无效。

Still try below function to stop timer, after assigning timer variable . 在分配计时器变量后 ,仍尝试使用以下功能停止计时器。

@IBAction func stop(_ sender: UIButton) {

       start = false
       timer.invalidate()
       timer = nil
       time = 0
    }

Hope this work 希望这项工作

First you have to declare it as fileprivate as below: 首先,您必须将其声明为fileprivate,如下所示:

fileprivate var timer = Timer()

@IBAction func start(_ sender: UIButton) {

        self.timer = Timer.scheduledTimer(timeInterval: 4,
                                                      target: self,
                                                      selector: #selector(self.updateTimerForLocation),
                                                      userInfo: nil,
                                                      repeats: true)

}

@IBAction func stop(_ sender: UIButton) {

   timer.invalidate()


}

you are confusing between variable stop and func stop. 您在变量停止和功能停止之间感到困惑。
also you dont need 2 parameter to manage stop/start status 你也不需要2参数来管理停止/开始状态

-- for your question, you should retain the timer variable to able to use it in other func declare a global timer -对于您的问题,您应该保留timer变量,以便能够在其他func中使用它声明全局计时器

var timer: Timer!

then 然后

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector:#selector(ViewController.action), userInfo: nil, repeats: true)

now you can invalidate it anywhere 现在您可以在任何地方使它失效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM