Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating a method every few seconds in Objective C

I have a method -(void)foo that I want to call once in viewDidAppear, then I want it to repeat every n seconds, where n is some integer. How can I do this?

I tried this, but it's not repeating:

[NSTimer scheduledTimerWithTimeInterval:7.0 target:nil selector:@selector(foo) userInfo:nil repeats:YES];
like image 939
Snowman Avatar asked Oct 08 '11 23:10

Snowman


1 Answers

The problem is that target is nil.

What this timer does is every 7 seconds it calls [nil foo], which does nothing.

If you want to call the method foo on the object that creates the timer, use target:self

like image 85
cobbal Avatar answered Sep 20 '22 15:09

cobbal