我有点想做什么,程序运行,但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)更多推荐
发布评论