1、
- (void)viewDidLoad { [super viewDidLoad]; //定时器 反复执行 NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; }
2、
-(void)updateTime{ UILabel *timeLable = [[UILabel alloc]initWithFrame:CGRectMake(0,0,200,30)]; timeLable.backgroundColor = [UIColor clearColor]; [self.view addSubview:timeLable]; NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; NSLog(@"现在时间:%@",dateString); timeLable.text = dateString; }