我想在标签上显示当前时间,时间格式为 hh:mm:ss

tec*_*hno 1 objective-c nsdate nsdateformatter ios

当前时间格式应该像 hh:mm:ss 一样,秒应该运行,秒不应该停止计数,它应该继续运行让我向你展示我正在编写的代码。

-(void)timeNotify
{
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:currDate];

_timeLbl.text=dateString;

}
Run Code Online (Sandbox Code Playgroud)

鉴于确实加载了我称之为

-(void)viewDidLoad
{
  [self timeNotify];
}
Run Code Online (Sandbox Code Playgroud)

小智 5

你应该使用NSTimer

- (void)viewDidLoad
{
  [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(timeNotify)
                                   userInfo:nil
                                    repeats:YES];
}

- (void) timeNotify
{
    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm:ss"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];
    _timeLbl.text = resultString;
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

1205 次

最近记录:

9 年,11 月 前