NSTimer와 NSDate가 함께 작동하여 카운트 다운 시계를 만듭니다. 그러면 UILabel이 카운트 다운으로 업데이트됩니다.NSTimer 및 NSDate 카운트 다운 시계는 시뮬레이터에서 작동하지만 장치에는 없습니다
내가 직면 한 문제는 카운트 다운 시계가 시뮬레이터에서 작동하지만 장치에서는 작동하지 않는다는 것입니다. 시뮬레이터 iOS 6.0 및 iOS 6.0.1 장치.
시뮬레이터 콘솔 출력 :
2012-11-14 15:39:57.021 appName[7856:13d03] 31 hours : 20 min
2012-11-14 15:39:58.020 appName[7856:13d03] 31 hours : 20 min
2012-11-14 15:39:59.021 appName[7856:13d03] 31 hours : 19 min
2012-11-14 15:40:00.021 appName[7856:13d03] 31 hours : 19 min
장치 콘솔 출력 :
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self startTimer];
}
// timer and update label to display a countdown clock
- (void)startTimer {
// Create a timer that fires every second repeatedly and save it in an ivar
NSTimer *timer = [[NSTimer alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
- (void)updateLabel {
// convert date string to date then set to a label
NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
[dateStringParser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.000Z"];
NSDate *date = [dateStringParser dateFromString:deadlineDate];
NSLog(@"date from update label %@", date);
NSTimeInterval timeInterval = [date timeIntervalSinceNow]; ///< Assuming this is in the future for now.
NSString *stringVariable = [self stringFromTimeInterval:timeInterval];
self.deadlineLbl.text = [NSString stringWithFormat:@"%@", stringVariable];
NSLog(@"%@",stringVariable);
}
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
NSInteger ti = (NSInteger)interval;
// NSInteger seconds = ti % 60;
NSInteger minutes = (ti/60) % 60;
NSInteger hours = (ti/3600);
return [NSString stringWithFormat:@"%02i hours : %02i min", hours, minutes];
}
여기에 몇 가지 콘솔 출력 장치에 먼저 시뮬레이터에 다음입니다 : 여기
내 코드입니다2012-11-14 15:45:44.887 appName[7556:907] 00 hours : 00 min
2012-11-14 15:45:45.896 appName[7556:907] 00 hours : 00 min
2012-11-14 15:45:46.891 appName[7556:907] 00 hours : 00 min
2012-11-14 15:45:47.891 appName[7556:907] 00 hours : 00 min
다른 문제는 사용자가보기를 떠난 후 타이머가 계속 업데이트된다는 것입니다. 줄에 날짜 변수에 대한 도움
[self stringFromTimeInterval]은 무엇을합니까? 코드를 게시 할 수 있습니까? iOS가 시뮬레이터 및 장치에 사용됩니까? – meronix
나는 편집을했다. 시뮬레이터와 장치 모두에서 iOS 6.0. 나는 또한 누락 된 코드를 게시했다. 감사합니다 :) – hanumanDev
countdownDate의 로그를 보여주는 것은 무엇입니까? 기기에서 유효합니까? –