我必须为特定间隔运行倒计时器,但如何在此功能中提供self.startDate
- (void)updateCounter:(NSTimer *)theTimer {
NSDate *now = [NSDate date];
// has the target time passed?
if ([self.startDate earlierDate:now] == self.startDate) {
[theTimer invalidate];
} else {
NSUInteger flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:now toDate:self.startDate options:0];
NSLog(@"there are %d hours, %d minutes and %d seconds remaining", [components hour], [components minute], [components second]);
}
}
答案 0 :(得分:1)
使用以下方法将NSString转换为NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:yourString];