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

I want to show current time on a label with time format hh:mm:ss

当前时间格式应该是 hh:mm:ss 秒应该是 运行 ,秒不应该停止计数,它应该继续 运行ning 让我告诉你我的代码正在写。

-(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;

}

并且在视图中确实加载了我称之为

-(void)viewDidLoad
{
  [self timeNotify];
}
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(@"User's current time in their preference format:%@",currentTime);

如果您希望定期更新您的时间,您应该使用 NSTimer

NSTimer* timer =       [NSTimer scheduledTimerWithTimeInterval:1
                                   target:self
                                 selector:@selector(timeNotify)
                                 userInfo:nil
                                  repeats:YES]; 

请记住在视图消失时将 timer 设置为 nil,因为计时器一直引用您的 ViewController,因此如果您不销毁它会导致内存泄漏

你应该使用 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;
}