views:

635

answers:

3

I was wondering whether there is a solution to raise an event once after 30 seconds or every 30 seconds in CocoaTouch ObjectiveC.

+5  A: 

There are a number of options.

The quickest to use is in NSObject:

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

(There are a few others with slight variations.)

If you want more control or to be able to say send this message every thirty seconds you probably need NSTimer.

Stephen Darlington
+3  A: 

+[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]

Documentation

You may also want to look at the other NSTimer methods

JoostK
+2  A: 

Take a look at the NSTimer class:

NSTimer *timer;
...
timer = [[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(thisMethodGetsFiredOnceEveryThirtySeconds:) userInfo:nil repeats:YES] retain];
[timer fire];

Somewhere else you have the actual method that handles the event:

- (void) thisMethodGetsFiredOnceEveryThirtySeconds:(id)sender {
   NSLog(@"fired!");
}
Alex Reynolds