Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current time and keeping it updated - Objective C

I know how to use NSDate to get the time and display it inside UILabel.

i need to display the date + hours and minutes. any idea how can i keep it updated without busy-waiting?

Thanks!

like image 588
Chiko Avatar asked Feb 17 '23 03:02

Chiko


1 Answers

Use NSTimer to update time on the label

- (void)viewDidLoad
 {
  [super viewDidLoad];

   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
 }



-(void)updateTime
{


NSDate *date= [NSDate date];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init]; //for hour and minute

formatter1.dateFormat = @"hh:mm a";// use any format 

clockLabel.text = [formatter1 stringFromDate:date];

[formatter1 release];


}
like image 172
Anil Varghese Avatar answered Feb 20 '23 01:02

Anil Varghese