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
2009-09-16 09:31:48
+3
A:
+[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]
You may also want to look at the other NSTimer methods
JoostK
2009-09-16 09:31:59
+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
2009-09-16 09:33:57