BalestraPatrick
BalestraPatrick

Reputation: 10144

Update label with new NSDate

I have a the today date in a NSDate and I want to display this NSDate in a label. I want to change the label text with the new NSDate every time the seconds changes. I tried to use a NSTimer but it isn't synchronized with the iPhone clock. Is there any specific thing to directly have the label changed?

Upvotes: 1

Views: 1881

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

You can create a property timer as :

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5f
                                         target:self
                                       selector:@selector(showTime)
                                       userInfo:NULL
                                        repeats:YES];

And then this method will be called every half of a second.

- (void)showTime{
    NSDate *now=[NSDate date];
    NSDateFormatter *dateFormatter=[NSDateFormatter new];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    timeLabel.text=[dateFormatter stringFromDate:now];
}

Upvotes: 5

Related Questions