Saturday, April 27, 2013

NSTimer timerWithTimeInterval target selector userInfo repeats example ios


timerWithTimeInterval :target:selector:userInfo:repeats:

Creates and returns a new NSTimer object initialized with the specified object and selector.
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
target
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.[NSTimer timerWithTimeInterval]
aSelector
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.
userInfo
Custom user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Return Value of [NSTimer timerWithTimeInterval]
A new NSTimer object, configured according to the specified parameters.
Discussion of [NSTimer timerWithTimeInterval]
You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
Example of [NSTimer timerWithTimeInterval]
{
NSLog(@"Connecting");
timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
[Connect setStringValue:@"a"]; 
connected = YES;
NSLog(@"Finished\n");
}

- (void)timerFireMethod:(NSTimer*)theTimer
{
    NSLog(@"Fireing event");
}