Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS NSTimer not working? [duplicate]

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");
}
like image 275
Redneys Avatar asked Aug 17 '12 00:08

Redneys


1 Answers

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.

like image 185
Mick MacCallum Avatar answered Oct 15 '22 22:10

Mick MacCallum