我对 iOS 开发很陌生,所以希望你能帮助我!我现在正在使用 Xcode5-DP4 并且我的代码不能正常工作(虽然它以前工作得很好)。通过点击按钮开始倒计时 2:00(分:秒):
FirstViewController.h:
@interface FirstViewController : UIViewController {
IBOutlet UILabel *countdownLabel;
IBOutlet UIButton *countdownStart;
NSTimer *countdownTimer;
int secondsCount;
}
-(IBAction)alert;
-(void)delay;
FirstViewController.m:
UIAlertView *alert;
-(void)delay {
[alert show];
}
-(IBAction)alert{
alert = [[UIAlertView alloc]
initWithTitle:@"Die Zeit ist um!"
message:@"Du darfst das Zähneputzen nun beenden, aber vergiss nicht, noch mehr für deine Mundhygiene zu tun."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[self performSelector:@selector(delay) withObject:nil afterDelay:120.0];
}
- (IBAction)zaehneputzen:(id)sender {
[self setTimer];
}
- (void) setTimer {
secondsCount = 120;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:(self) selector:@selector(timerRun) userInfo:(nil)
repeats:(YES)];
}
- (void) timerRun {
secondsCount = secondsCount-1;
int minutes = secondsCount / 60;
int seconds = secondsCount - (minutes * 60);
NSString *timerOutput = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
countdownLabel.text = timerOutput;
if(secondsCount == 0){
[countdownTimer invalidate];
countdownTimer = nil;
}
}
问题: - 计时器计数太快并且没有使用完整的秒数(标签在 0.5 秒后和 1.5 秒后发生变化,依此类推......) - 计时器继续计数到负数范围......
我希望你能帮助我=)。
在此先感谢,安德里亚!