NSTimer timerWithTimeInterval: not working

后端 未结 4 1511
挽巷
挽巷 2020-11-28 13:21

I\'ve created a test application with timer before implementing it in my project. It was the first time I\'m using timer. But the issue is when I implemented timer using

4条回答
  •  时光说笑
    2020-11-28 13:45

    scheduledTimerWithTimeInterval:invocation:repeats: and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: create timers that get automatically added to an NSRunLoop, meaning that you don't have to add them yourself. Having them added to an NSRunLoop is what causes them to fire.

    With timerWithTimeInterval:invocation:repeats: and timerWithTimeInterval:target:selector:userInfo:repeats:, you have to add the timer to a run loop manually, with code like this:

    [[NSRunLoop mainRunLoop] addTimer:repeatingTimer forMode:NSDefaultRunLoopMode];
    

    Other answers on here suggest that you need to call fire yourself. You don't - it will be called as soon as the timer has been put on a run loop.

提交回复
热议问题