Objective c NSDate调用一个方法
我是新的编码,请原谅我的无知 背景: 我有一个应用程序,它有一个简单的标签,可以按天、小时、分、秒数到给定的日期。 我使用了Objective c NSDate调用一个方法,objective-c,cocoa-touch,nsdate,nstimer,Objective C,Cocoa Touch,Nsdate,Nstimer,我是新的编码,请原谅我的无知 背景: 我有一个应用程序,它有一个简单的标签,可以按天、小时、分、秒数到给定的日期。 我使用了NSDate和NSTimers: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //Countdown timer destinationDate = [N
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 h.s”之类的内容吗
我的意思是,你已经有了updateLabel方法 最好的方法是查看当前日期是否晚于或等于
destinationDate
if ([destinationDate timeIntervalSinceNow] <= 0) {
// The destination date is in the past - do something
}
如果([destinationDate timeIntervalSinceNow]如果您希望条件检查倒计时计时器是否达到零,请尝试以下条件:-
if ([destinationDate compare:[NSDate
date]]==NSOrderedSame)
//hiding your object
}
旁注-为什么要在格式字符串中使用%c
表示h
,m
等?只需将字母像冒号一样放入格式字符串中。是的,标签等于给定日期的倒计时,所以它会显示180天10小时30分钟2秒,然后每秒倒计时。这不起作用k、 日期以毫秒为单位存储,两者完全相等的概率基本上为零。