我有一个简单的计时器,按下按钮即可激活它。它从 60 运行到 0 没问题,但我想要的是停止并重置按钮上的计时器。我已经设法使用下面的代码在按下按钮时停止它,但由于某种原因无法让它重置并停止在 60 处。这应该很简单,但它不起作用。有什么建议么?
使用简单的 Action 设置计时器
- (IBAction)timerStart:(id)sender {
if(!secondCounter == 0){
[countdownTimer invalidate];
}
else {
[self setTimer];
}
}
定时器代码
- (void)timerRun {
secondCounter = secondCounter - 1;
int minutes = secondCounter;
NSString *timerOutput = [NSString stringWithFormat:@"%d", minutes ];
countDownLabel.text = timerOutput;
if(secondCounter == 0){
[countdownTimer invalidate];
countdownTimer = nil;
}
}
- (void)setTimer {
secondCounter = 60;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
您需要将秒设置为 0,否则您总是使计时器无效,但永远不会再次启动它:
- (IBAction)timerStart:(id)sender {
if(!secondCounter == 0) {
[countdownTimer invalidate];
secondCounter = 0;
}
else {
[self setTimer];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)