我有一個簡單的計時器使用,是用一個按鈕被激活。它從60運行到0沒有問題,但我想要停止並按下按鈕重置定時器。我已經設法停止它,當按下按鈕使用下面的代碼,但出於某種原因不能讓它重置並在60停止。這應該很簡單,但它不工作。有什麼建議麼?停止和重置NSTimer
定時器使用簡單的動作
- (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];
}
謝謝..它對我很有用 – Aayushi