尝试切换 2 个 NSTimers

发布于 2024-10-08 05:53:39 字数 2520 浏览 9 评论 0原文

我正在尝试在我的应用程序中添加一项功能,允许用户将以美国格式 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-10-15 05:53:39

您在这里展示的设计不必要地复杂。首先,您要求以百分之一秒的间隔来更新表示当前时间的标签。这大约是发射频率的一百倍。

您的设计应该是这样的:

  • NSDateFormatter 属性添加到您的视图控制器类中。你不需要暴露
    该财产公开。
  • 在viewDidLoad中,
    • alloc/init 您的 NSDateFormatter 属性为您想要的任何默认设置,美国或英国(您可以从 <代码>NSUserDefaults。
    • 每隔 1 秒启动一个计时器,调用更新标签的方法。此方法应使用 NSDateFormatter 属性,无论它是什么值(该方法不关心)来呈现日期/时间
  • 在回调方法中切换时间格式,
    • release NSDateFormatter 属性并alloc/init一个新属性
  • 在 viewDidUnload 中,
    • 无效/释放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:

  • Add an NSDateFormatter property to your view controller class. You don't need to expose
    this property publicly.
  • In viewDidLoad,
    • alloc/init your NSDateFormatter property to whatever default setting you want, US or UK (you might read/write this preference from NSUserDefaults.
    • Start a timer on an interval of 1 second to call a method to update the label. This method should use the NSDateFormatter property, whatever value it is (the method doesn't care) to render the date/time
  • In your callback method to toggle the time format,
    • release the NSDateFormatter property and alloc/init a new one
  • In viewDidUnload,
    • invalidate/release the NSTimer object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文