iPhone - NSTimer在火灾后不再重复 [英] iPhone - NSTimer not repeating after fire

查看:89
本文介绍了iPhone - NSTimer在火灾后不再重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建并触发 NSTimer ,其中包含:

I am creating and firing a NSTimer with:

ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(handleTimer:)
                                         userInfo:nil
                                          repeats:YES];
[ncTimer fire];

AND

- (void)handleTimer:(NSTimer *)chkTimer {
    // do stuff
}

我保留我的计时器:

@property (nonatomic, retain) NSTimer *ncTimer;

由于某种原因,计时器不会重复。它只发射一次而不是再发射一次。

For some reason the timer is not repeating. It is firing once only and than never again.

推荐答案

您不能只将分配给已作为属性放入标题的计时器。这应该工作:

You can't just assign to the timer that you have put as a property in your header. This should work:

self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(handleTimer:) userInfo:nil repeats: YES];

另外:fire方法触发定时器,不在循环中。如果计时器不重复,则无效。在说明火的行之后,添加:

Also: The fire method fires the timer, out of cycle. If the timer is non repeating it is invalidated. After the line that says fire, add this:


BOOL timerState = [ncTimer isValid];
NSLog(@"Timer Validity is: %@", timerState?@"YES":@"NO");

这篇关于iPhone - NSTimer在火灾后不再重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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