Reputation: 6051
I have created a custom class which show time date formatter and I need a timer like method to update the seconds, so here is my code :
CustomClass.m
- (NSString *) showLocaleTime {
NSDateFormatter *timeFormater = [[NSDateFormatter alloc] init];
timeFormater = [setDateFormat:@"HH:mm:ss "];
NSString *currDay = [timeFormater stringFromDate:[NSDate date]];
currDay = [NSString stringWithFormat:@"%@",currDay];
[timeFormater release];
return timer;
}
- (void) updateLocaleTime {
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showLocaleTime) userInfo:nil repeats:YES];
}
viewController.m :
CustomClass *time = [[CustomClass alloc]init];
label.text = [time showLocaleTime];
[time updateLocaleTime];
But the problem is the updateLocaleTime
does not call to update seconds ! am I missing something ?
Thanks
Upvotes: 0
Views: 200
Reputation: 17478
Instead of calling updateLocaleTime
in CustomClass
, just start the timer in the view controller itself.
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateLocaleTime) userInfo:nil repeats:YES];
Add updateLocaleTime
method to the viewController
- (void) updateLocaleTime {
CustomClass *time = [[CustomClass alloc]init];
label.text = [time showLocaleTime];
[time release];
}
But here we are allocating and releasing the CustomClass
again and again for every 0.5 seconds. Instead you declare it as class member, in .h file and allocate that in viewDidLoad
.
So no need to allocate in updateLocaleTime
method. Also release that time
in viewDidUnload
method.
Upvotes: 1
Reputation: 47699
Where do you update the label text, after computing the new time? (You compute then new time but the it falls on the floor.)
Eg, add label.text = timer;
to your showLocalTime method, and skip returning timer
.
Upvotes: 0