尝试切换 2 个 NSTimers
我正在尝试在我的应用程序中添加一项功能,允许用户将以美国格式 (MM/DD/YYYY) 显示的当前日期切换为英国格式 (DD/MM/YYYY) 并返回。
我正在使用分段控件在两个单独的计时器(dateTimer_us 和 dateTimer_uk)之间切换 UILabel (dateSlateLabel)。
我已经一切正常,但每当我切换到 dateTimer_uk 时,dateTimer_us 就会不断反击,就好像我没有杀死它一样。当我说反击时,我的意思是我看到英国格式和美国格式根据计时器的时间间隔来回闪烁。因此,由于我的计时器的 timeInterval 为 0.01,我看到美国格式在英国格式上快速闪烁。我很清楚 dateTimer_us 并没有失效,或者有其他东西正在启动它。当我切换到 dateTimer_us 时,一切似乎都很好,问题恰恰相反(当我切换到 dateTimer_uk 时)。我认为我的代码/内存管理出了问题,但我无法弄清楚。我在网上搜索了一下这个问题,花了两天时间。
有什么想法吗?
注意:我尝试过仅使用一个计时器和一个 if else 检查segmentControl状态来驱动日期格式,但这似乎不起作用。
这是我的代码:
//////////////////////////////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]];
}
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]];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里展示的设计不必要地复杂。首先,您要求以百分之一秒的间隔来更新表示当前时间的标签。这大约是发射频率的一百倍。
您的设计应该是这样的:
NSDateFormatter
属性添加到您的视图控制器类中。你不需要暴露该财产公开。
alloc
/init
您的NSDateFormatter
属性为您想要的任何默认设置,美国或英国(您可以从 <代码>NSUserDefaults。NSDateFormatter
属性,无论它是什么值(该方法不关心)来呈现日期/时间release
NSDateFormatter
属性并alloc
/init
一个新属性无效
/释放
NSTimer
对象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:
NSDateFormatter
property to your view controller class. You don't need to exposethis property publicly.
alloc
/init
yourNSDateFormatter
property to whatever default setting you want, US or UK (you might read/write this preference fromNSUserDefaults
.NSDateFormatter
property, whatever value it is (the method doesn't care) to render the date/timerelease
theNSDateFormatter
property andalloc
/init
a new oneinvalidate
/release
theNSTimer
object