NSTimer not calling Method [duplicate] NSTimer not calling Method [duplicate] swift swift

NSTimer not calling Method [duplicate]


Either use

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

or

//schedules the timer[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];

From the docs Scheduling Timers in Run Loops


Swift Code

Either

let timer: NSTimer = NSTimer(timeInterval: 1.0, target: self, selector: "fadeManager:", userInfo: nil, repeats: true)NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)

Or

//schedules the timerNSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "fadeManager:", userInfo: nil, repeats: true)


Your problem is that when using timerWithTimeInterval:target:selector:userInfo:repeats:, the resulting timer does not automatically get added to the run loop. I would recommend using scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: instead, which performs this step for you.

If you prefer to use timerWithTimeInterval:target:selector:userInfo:repeats: then you need to manually add the timer to the current run loop. To do this, call NSRunLoop's addTimer:forMode: method. Documentation

[[NSRunLoop currentRunLoop] addTimer:tTimer forMode: NSDefaultRunLoopMode];


You need to fire the timer.

You can do it by adding it to a thread:

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

or calling

[timer fire];