我想在标签中显示实时,我找到了两种更新方法NSDate
:
performSelector
方法:
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateTime];
}
- (void)updateTime
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mm:ss"];
label.text = [dateFormat stringFromDate:[NSDate date]];
//call updateTime again after 1 second
[self performSelector:@selector(updateTime) withObject:self afterDelay:1.0];
}
NSTimer
方法:
- (void)viewDidLoad
{
[super viewDidLoad];
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}
- (void)updateTime
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mm:ss"];
label.text = [dateFormat stringFromDate:[NSDate date]];
}
哪一种是更好、更高效、更可靠的方法?