开发者

iPhone - NSTimer not repeating after fire

开发者 https://www.devze.com 2023-02-06 19:17 出处:网络
I am creating and firing a NSTimer with: ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self

I am creating and firing a NSTimer with:

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

AND

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

I am retaining my timer with:

@property (nonatomic, retain) NSTimer *ncTimer;

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


The -fire: method manually fires it once. For a timer to be started and repeat, you have to add it to a runloop using [[NSRunLoop currentRunLoop] addTimer: forMode:]


Got it

Adding timer to mainRunLoop made it working

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号