ios 如何停止 NSTimer.scheduledTimerWithTimeInterval

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29743961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:42:54  来源:igfitidea点击:

How to stop NSTimer.scheduledTimerWithTimeInterval

iosswiftnstimer

提问by Erik Auranaune

How do i stopmy timer from running? Not like a pause, but a stop.

如何阻止我的计时器运行?不像暂停,而是停止。

import UIKit

class LastManStandingViewController: UIViewController {    

@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var timeTextbox: UITextField!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stopButton: UIButton!

var myCounter = 0
var myTimer : NSTimer = NSTimer()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    timeLabel.text = String(myCounter)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func startTimer(){
    myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)
    println("func startTimer")
}

func stopTimer(){
    myTimer.invalidate()
    myCounter = 0
    timeLabel.text = String(myCounter)
    println("func stopTimer")
}

func updateTimer(){
    timeLabel.text = String(myCounter++)
    println("func updateTimer")
}
@IBAction func startButton(sender: AnyObject) {
    startTimer()
}
@IBAction func stopButton(sender: AnyObject) {
    stopTimer()
}

}

}

I can start the timer, but when i press the Stop button, it reset itself, and starts counting again. It doesn't stop.

我可以启动计时器,但是当我按下停止按钮时,它会自行重置,然后再次开始计数。它不会停止。

Made it work. Something was buggy with my project! Fixed it by removing the button and re-adding them. Looks like i had a duplicate or something.

让它工作。我的项目出了点问题!通过删除按钮并重新添加它们来修复它。看起来我有一个重复的东西。

回答by Eric Aya

You don't have to use Selector:

您不必使用Selector

@IBAction func startButton(sender: AnyObject) {
    myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
}

Also, the timer passes itself to the selected method, so you can invalidate it inside the method if you need:

此外,计时器将自身传递给选定的方法,因此如果需要,您可以在方法内使其无效:

func updateTimer(timer: NSTimer) {
    timeLabel.text = String(Counter++)
    timer.invalidate()
}

Or if the timer is an instance variable:

或者,如果计时器是一个实例变量:

myTimer.invalidate()
myTimer = nil

It's a good thing to nilthe instance variable timer after having invalidated it, it avoids further confusion if you need to create another timer with the same variable. Also, method names and variables should begin with a lowercase letter.

nil实例变量 timer 在使其无效后是一件好事,如果您需要使用相同的变量创建另一个计时器,它可以避免进一步的混乱。此外,方法名称和变量应以小写字母开头。

Screenshot to show the timer invalidated and set to nil.

屏幕截图显示计时器失效并设置为 nil。

screenshot

截屏

Update for Swift 2.2+

Swift 2.2+ 更新

See https://stackoverflow.com/a/36160191/2227743for the new #selectorsyntax replacing Selector().

有关新的语法替换,请参阅https://stackoverflow.com/a/36160191/2227743#selectorSelector()

回答by Dharmesh Kheni

you can use this when some condition is met and you want to stop timer:

当满足某些条件并且您想停止计时器时,您可以使用它:

Timer.invalidate()

Here is simple example :

这是一个简单的例子:

func UpdateTimer(){
    timeLabel.text = String(Counter++)
    if timeLabel.text == String("5") {
        Timer.invalidate()
    }
}

this will stop timer.

这将停止计时器。

You can modify it as per your need.

您可以根据需要对其进行修改。

回答by Gautam Sareriya

As pre Swift 2.2

作为 Swift 2.2 之前的版本

let printTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(printDescription), userInfo: nil, repeats: true)

func printDescription() {
    print("Print timer will print every after 2.0 seconds")
}

Print timer will print every after 2.0 seconds

打印计时器将每 2.0 秒打印一次