NSTimer()

编程入门 行业动态 更新时间:2024-10-26 21:19:18
NSTimer() - timer.invalidate不能用于简单的秒表吗?(NSTimer() - timer.invalidate not working on a simple stopwatch?)

我有点想做什么,程序运行,但timer.invalidate()不起作用? 欢迎任何提示。

我过去使用过这个程序并通过它完成了,timer.invalidate()完美地工作了。 我不相信它与我把它放入函数的事实有关,因为在我只是输入“timer.invalidate()”而不是“stop()”之前它没有工作

每当我点击iOS模拟器上的取消按钮时,它只会将其重置为0,但会继续计数。

提前致谢。

import UIKit class ViewController: UIViewController { var timer = NSTimer() var count = 0 @IBOutlet weak var timeDisplay: UILabel! @IBAction func playButton(sender: AnyObject) { //play button var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true) } @IBAction func resetTimer(sender: AnyObject) { //cancel button stop() count = 0 timeDisplay.text = "0" } @IBAction func pauseButton(sender: AnyObject) { stop() } func result() { count++ timeDisplay.text = String(count) } func stop () { timer.invalidate() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

I'm kind of at a wall of what to do, the program runs, but the timer.invalidate() does not work? Any tips are welcome.

I worked with this program in the past and worked through it and the timer.invalidate() worked flawlessly. I do not believe that it has to do with the fact that I put it into a function because before it wasn't working when I was just typing "timer.invalidate()" instead of "stop()"

Whenever I click the button Cancel on the iOS simulator it just resets it back to 0, but keeps counting.

Thanks in advance.

import UIKit class ViewController: UIViewController { var timer = NSTimer() var count = 0 @IBOutlet weak var timeDisplay: UILabel! @IBAction func playButton(sender: AnyObject) { //play button var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true) } @IBAction func resetTimer(sender: AnyObject) { //cancel button stop() count = 0 timeDisplay.text = "0" } @IBAction func pauseButton(sender: AnyObject) { stop() } func result() { count++ timeDisplay.text = String(count) } func stop () { timer.invalidate() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

最满意答案

在playButton()你定义了另一个定时器,它不能从这个函数外部失效 - 所以通过调用timer.invalidate()你只使var timer = NSTimer()无效,它不带任何set timer。

所以更换

var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

In playButton() you are defining another timer, that can't be invalidated from outside this function - so by calling timer.invalidate() you invalidate just var timer = NSTimer() which doesn't carry any set timer in it.

So replace

var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

with

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

更多推荐

本文发布于:2023-08-07 23:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSTimer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!