Saturday, April 27, 2013

NSTimer initWithFireDate example ios


initWithFireDate :interval:target:selector:userInfo:repeats:

Initializes a new NSTimer object using the specified object and selector.
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
Parameters
date
The time at which the timer should first fire.
seconds
For a repeating timer, this parameter contains 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.[NSTimer initWithFireDate]
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 initWithFireDate]
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 initWithFireDate]
The receiver, initialized such that, when added to a run loop, it will fire at date and then, if repeats is YES, every secondsafter that.
Discussion of [NSTimer initWithFireDate]
You must add the new timer to a run loop, using addTimer:forMode:. Upon firing, the timer sends the messageaSelector 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 initWithFireDate]
NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setDay:24];
[comps setMonth:10];
[comps setYear:2011];

[comps setHour:23];
[comps setMinute:11];
[comps setSecond:0];


NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
schedulerTimer=[[NSTimer alloc] initWithFireDate:date interval:0 target:self selector:@selector(recordByScheduler:) userInfo:nil repeats:false];
[[NSRunLoop currentRunLoop] addTimer:schedulerTimer forMode:NSDefaultRunLoopMode];