不能失效,停止倒计时 NSTimer - Objective C [英] Can't invalidate, stop countdown NSTimer - Objective C

查看:84
本文介绍了不能失效,停止倒计时 NSTimer - Objective C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是停止 NSTimer,出于某种原因 [Timer invalidate] 只是不工作...

Problem is to stop NSTimer, for some reason [Timer invalidate] just not working...

也许我的眼睛里满是肥皂水,但不明白为什么计时器没有停在0,而是倒数-1、-2、-3等等...(((

Maybe my eyes are full of soap, but can't understand the reason why the timer didn't stop at 0, but go reverse counting -1, -2, -3 and so on...(((

我使用纪元数作为目标日期.还有一件事 - 我的按钮IBAction stop"和 [Timer invalidate] 工作得很好 - 当我在模拟器中按下它时计时器停止......

I'm using epoch numbers as destination date. One more thing - my button "IBAction stop" with [Timer invalidate] works just fine - when i push it in simulator timer stops...

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];


Timer = [NSTimer  scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

}

- (IBAction) start {

Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];


}
- (IBAction) stop {

[Timer invalidate];
Timer = nil;

}

-(void)updateLabel {

NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calender components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d%c  %d%c  %d%c  %d%c", [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];

destinationDate = [NSDate dateWithTimeIntervalSince1970:1355299710];

if (!destinationDate) {

    [Timer invalidate];
    Timer = nil;
}
}

推荐答案

正如 Totumus 所指出的,你的 if 语句条件 !destinationDate 总是评估为假,所以你的 updateLabel 方法永远不会使您的计时器无效.

As Totumus pointed out, your if statement condition !destinationDate always evaluates to false, so your updateLabel method never invalidates your timer.

你也有另一个错误:

您正在 viewDidLoad 中创建一个计时器,并将对它的引用存储在您的 Timer 实例变量中.

You're creating a timer in viewDidLoad and storing a reference to it in your Timer instance variable.

然后您在 start 中创建 另一个 计时器并将对它的引用存储在您的 Timer 实例变量中,覆盖对您在 viewDidLoad 中创建的计时器,而不会使旧计时器失效.

Then you're creating another timer in start and storing a reference to it in your Timer instance variable, overwriting the reference to the timer you created in viewDidLoad without invalidating that older timer.

所以现在您有两个计时器正在运行,但您没有对旧计时器的引用,因此您永远无法使其无效.

So now you have two timers running, but you don't have a reference to the older timer, so you can never invalidate it.

请注意,run loop 有一个对预定(正在运行)计时器的强引用,因此即使您删除了对它的所有强引用,计时器仍会继续运行.这就是 invalidate 消息存在的原因:告诉运行循环删除它对计时器的强引用.

Note that the run loop has a strong reference to a scheduled (running) timer, so even if you remove all of your strong references to it, the timer keeps running. That's why the invalidate message exists: to tell the run loop to remove its strong reference to the timer.

这篇关于不能失效,停止倒计时 NSTimer - Objective C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆