【问题标题】:How to stop NSTimer.scheduledTimerWithTimeInterval如何停止 NSTimer.scheduledTimerWithTimeInterval
【发布时间】:2015-06-26 22:43:29
【问题描述】:

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

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

}

我可以启动计时器,但是当我按下停止按钮时,它会自行重置并重新开始计时。它不会停止。

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

【问题讨论】:

  • 请贴出你的代码(如果只是截图我们无法测试)。
  • @ericd 现已添加完整代码。
  • 我已经在示例应用程序中复制粘贴了您的代码的重要部分,我没有发现任何问题,计时器可以正确启动和停止。你确定你没有做其他事情吗? screenshot of my test
  • 您的代码对我来说运行良好。
  • @ericd 好的,所以我删除了开始和停止按钮并再次添加它们,现在一切正常。奇怪!

标签: ios swift nstimer


【解决方案1】:

您不必使用Selector

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

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

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

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

myTimer.invalidate()
myTimer = nil

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

显示计时器无效并设置为零的屏幕截图。

Swift 2.2+ 更新

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

【讨论】:

    【解决方案2】:

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

    Timer.invalidate()
    

    这是一个简单的例子:

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

    这将停止计时器。

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 问题是如何停止 NSTimer.scheduledTimerWithTimeInterval。
      猜你喜欢
      • 2015-06-07
      • 1970-01-01
      • 2016-01-08
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2012-06-20
      • 2018-07-05
      相关资源
      最近更新 更多