简体   繁体   English

如何停止NSTimer.scheduledTimerWithTimeInterval

[英]How to stop NSTimer.scheduledTimerWithTimeInterval

How do i stop my 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. 看起来我有重复或什么的。

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 nil the instance variable timer after having invalidated it, it avoids further confusion if you need to create another timer with the same variable. 在使实例变量计时器失效后将其置nil是一件好事,如果您需要使用相同的变量创建另一个计时器,则可以避免进一步的混淆。 Also, method names and variables should begin with a lowercase letter. 此外,方法名称和变量应以小写字母开头。

Screenshot to show the timer invalidated and set to nil. 屏幕截图显示计时器无效并设置为nil。

截图

Update for Swift 2.2+ Swift 2.2+的更新

See https://stackoverflow.com/a/36160191/2227743 for the new #selector syntax replacing Selector() . 有关替换Selector()的新#selector语法,请参阅https://stackoverflow.com/a/36160191/2227743

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. 您可以根据需要进行修改。

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秒后打印

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

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