我是编码的新手,请原谅任何无知!
背景:
我有一个带有简单标签的应用,可以按天,小时,分钟和秒计算到给定日期。
我是使用NSDate
和NSTimer
s:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Countdown timer
destinationDate = [NSDate dateWithTimeIntervalSince1970:1413961418];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
-(void)updateLabel {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[countdownLabel setText:[NSString stringWithFormat:@"%ld%c : %ld%c : %ld%c : %ld%c", (long)[components day], 'd', (long)[components hour], 'h', (long)[components minute], 'm', (long)[components second], 's']];
}
问题:
当倒计时标签达到0时(即当我们到达给定日期时),我如何获得一个方法来触发,例如隐藏对象?
答案 0 :(得分:0)
也许我没有得到您的问题,但您能否验证标签文本是否等于“0:0:0 h.s”?
我的意思是,您已经拥有更新标签方法。
答案 1 :(得分:0)
最好的方法是查看当前日期是否晚于或等于destinationDate
。
if ([destinationDate timeIntervalSinceNow] <= 0) {
// The destination date is in the past - do something
}
答案 2 :(得分:0)
如果您想要条件检查倒数计时器是否达到零,请尝试以下条件: -
if ([destinationDate compare:[NSDate
date]]==NSOrderedSame)
//hiding your object
}