I’m trying to add a feature into my app that allows the user to toggle current date that is shown in US format (MM/DD/YYYY) to UK format (DD/MM/YYYY) and back.
I’m using a segment control to switch my UILabel (dateSlateLabel) between two separate timers (dateTimer_us & dateTimer_uk).
I’ve got everything working but whenever I toggle to the dateTimer_uk, dateTimer_us keeps fighting back, as if I haven’t killed it. When I say fighting back, I mean that I see the UK format with the US format blinking back and forth based on the timers’ timeInterval. So since my timers’ timeInterval is .01, I see a quick flashing of the US format over the UK format. Its clear to me that dateTimer_us is not being invalidated or something else is starting it back up. When I toggle to dateTimer_us, all seems fine, its just the other way around that is the problem, (when I toggle to dateTimer_uk). I figure something is wrong in my code/memory management but I can’t figure it out. I’ve scoured the web and spent two days on this problem.
Any ideas?
Note: I’ve tried using just one timer and an if else which checked the segmentControl status to drive the date format but that didn’t seem to work.
Here’s my code:
//////////////////////////////THIS CONTROLS THE DATE TIMER//////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)toggleDateFormat {
if(dateFormatSegmentControl.selectedSegmentIndex == 0) {
[dateTimer_uk invalidate];
[dateTimer_uk release];
dateTimer_uk = nil;
[self dateTimer_us];
}
else if (dateFormatSegmentControl.selectedSegmentIndex == 1) {
[dateTimer_us invalidate];
[dateTimer_us release];
dateTimer_us = nil;
[self dateTimer_uk];
}
}
-(void)dateTimer_us {
dateTimer_us = [[NSTimer scheduledTimerWithTimeInterval: .01
target: self
selector: @selector(displayDate_us)
userInfo: nil
repeats: YES] retain];
}
-(void)displayDate_us; {
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setDateFormat:@"MM/dd/yyyy"];
[dateSlateLabel setText:[formatter stringFromDate:date]];
}
-(void)dateTimer_uk {
dateTimer_uk = [[NSTimer scheduledTimerWithTimeInterval: .01
target: self
selector: @selector(displayDate_uk)
userInfo: nil
repeats: YES] retain];
}
-(void)displayDate_uk; {
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setDateFormat:@"dd/MM/yyyy"];
[dateSlateLabel setText:[formatter stringFromDate:date]];
}
The design you’ve shown here is needlessly complicated. For one, you’re asking for an interval of one hundredth of a second to update a label which represents the current time. This is roughly one hundred times too often to be firing.
Your design should be something like this:
NSDateFormatterproperty to your view controller class. You don’t need to exposethis property publicly.
alloc/inityourNSDateFormatterproperty to whatever default setting you want, US or UK (you might read/write this preference fromNSUserDefaults.NSDateFormatterproperty, whatever value it is (the method doesn’t care) to render the date/timereleasetheNSDateFormatterproperty andalloc/inita new oneinvalidate/releasetheNSTimerobject