我正在为iPhone构建一个简单的健身/训练应用程序。用户可以从表格中选择一种类型的训练课程,将其带到包含秒表的视图控制器。该视图的标签由可变数组填充。以不同的时间间隔更改uilabel文本
我可以让秒表工作,并从数组中填充初始标签,但无法弄清楚如何让标签在设定的时间间隔内发生变化。这些时间间隔将不会是正常的,所以可能会在10分钟,然后是25,然后是45等。我一直试图通过If语句来执行此操作,例如定时器== 25。我确信这是一个基本的解决方案,但我是编程新手,无法解决问题。
定时器的代码如下:
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
timerLabel.text = timeString;
}
启动计时器:
- (IBAction)startTimerButton:(id)sender {
if (timer == nil) {
startDate = [NSDate date];
// Create the stop watch timer that fires every 0.1s
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/10
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
}
else {
return;
}
}
到目前为止运行此代码的结果是什么? – 2012-03-23 15:15:24
没有生成错误和秒表正常工作。我无法弄清楚如何做If语句。我尝试了“if(timer == 5)[nextLable; setText ...]”,但是我得到一个错误'INT隐式转换为'NSTimer'不允许ARC' – pig70 2012-03-23 15:27:21
Try(timer.timeInterval == 5)。 阅读NSTimer的类参考https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html – 2012-03-23 15:33:40