NSTimer不发射 [英] NSTimer not firing
问题描述
我有一个NSTimer,我用这个代码:
I have an NSTimer that I init with this code:
testTimer = [[NSTimer alloc] initWithFireDate:[new objectAtIndex:0] interval:0.0 target:self selector:@selector(works:) userInfo:nil repeats:NO];
[new objectAtIndex:0]
NSDate过去。
[new objectAtIndex:0]
is an NSDate in the past.
当我启动应用程序时,计时器正在创建,使用fireDate立即(因为日期是过去),但它从不调用我的工作方法。 ( - (void)works:(id)sender
)
When I start up the app, the timer is getting created, with a fireDate of immediately (since the date is in the past), however it never calls my works method. (-(void)works:(id)sender
)
任何人都知道为什么会发生?
Anybody know why this is happening?
推荐答案
如果使用 initWith ...,你必须将它添加到当前运行循环中。 / code>方法创建计时器对象。
You will have to add it to the current run loop if you use initWith..
method to create the timer object.
NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];
[theRunLoop addTimer:testTimer forMode:NSDefaultRunLoopMode];
或者如果您希望为它设置,请使用 ..
创建您的计时器的方法。
Or if you would like it set up for you, use the scheduled...
methods to create your timer.
这篇关于NSTimer不发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!