나는 무슨 일이 일어나고 있는지 전혀 모른다. 나는 날짜를 비교하여 작동하는 타이머를 만들기 위해 노력하고 있습니다. 타이머를 시작하면 때때로 작동하고 무작위로 멈추며, 때로는 timeInterval의 값을 반환합니다. 시간 간격이 음수 일 때만 올바르게 작동하는 것 같습니다. 당신은 date
이전의 나중에, currentDate
을 뺀 것왜 내 timeinterval에 대해 음수 값이 표시됩니까?
-(IBAction)startTimer:(id)sender{
if (timer == nil) {
[startButton setTitle:@"Start" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
date = [NSDate dateWithTimeIntervalSinceNow:testTask.timeInterval]; //instance variable
} else {
[startButton setTitle:@"Stop" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
}
}
-(void)timerAction:(NSTimer *)t
{
NSDate *currentDate = [NSDate date];
NSTimeInterval updatedTimeInterval = [date timeIntervalSinceDate:currentDate];
if (updatedTimeInterval > 0){
if (self.timer)
{
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
}
else
{
testTask.timeInterval = updatedTimeInterval;
NSLog(@"%.2f", testTask.timeInterval);
NSError *error;
if (![self.context save:&error]) {
NSLog(@"couldn't save: %@", [error localizedDescription]);
}
}
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds/3600, (seconds/60) % 60, seconds % 60];
timerLabel.text = string;
NSLog(@"%f", testTask.timeInterval);
}
그냥 변경했습니다. 이제 Start를 누르면 타이머는 즉시 1193046 시간을 표시합니다 ... – EvilAegis
'date'는 타이머의 시작 시간을 저장하기 위해 사용하는 변수이고, 현재 날짜로 설정되어야합니다. - date = [ NSDate date];', 아니 dateWithTimeIntervalSinceNow : ' –