iOS NSTimer does not work?


This question already has an answer here:

  • NSTimer timerWithTimeInterval: not working 3 answers

This is my exact code, and it doesn't seem to be working. Can you tell me what I am doing wrong? Note that refreshTimer was already declared in the private interface.

-(void)viewDidLoad {
refreshTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerTest)      userInfo:nil repeats:YES];

-(void)timerTest {
NSLog(@"Timer Worked");

Give scheduledTimerWithTimeInterval a try:

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myMethod) userInfo:nil repeats:YES];

Quoting: NSTimer timerWithTimeInterval: not working

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.