【发布时间】:2014-08-26 19:19:32
【问题描述】:
我是编码新手,请见谅!
背景:
我有一个带有简单标签的应用程序,它以天、小时、分钟和秒为单位倒计时到给定日期。
我使用NSDate 和NSTimers 做到了这一点:
- (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 时(即当我们到达给定日期时)我怎样才能获得触发方法,例如隐藏对象?
【问题讨论】:
-
旁注 - 为什么在格式字符串中使用
%c表示h、m等?只需像冒号一样将字母放在格式字符串中。
标签: objective-c cocoa-touch nsdate nstimer