1. Initialization
+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;
+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;
Note: UserInfo is the user information that the value Nstimer carries.
without scheduled initialization, manual addtimer:formode is required: Add the timer to a runloop.
The scheduled initialization method will be added directly to the current runloop with the default mode.
Sample
[Nstimer scheduledtimerwithtimeinterval:2 Target:selfselector: @selector (startfindapartment:) UserInfo:nil repeats: YES];
Or
Nstimer *mytimer = [Nstimertimerwithtimeinterval: 3.0 target: Selfselector: @ Selector (timerfired:) userInfo: nilrepeats: NO];
[[Nsrunloopcurrentrunloop] addtimer: MyTimerformode: Nsdefaultrunloopmode];
2. Trigger (START)
When the timer is created (not scheduled, added to Runloop, the timer is automatically triggered after the timeinterval seconds specified at initialization.
Nstimer *timer=[nstimer timerwithtimeinterval:0.5target: Self selector:@selector(timeschedule)userInfo:Nil repeats:YES];
nsrunloop *runloop=[nsrunloopcurrentrunloop];
[Runloop addtimer: Timerformode:nsdefaultrunloopmode];
[Timer fire];
You can use the-(void) Fire method to trigger the timer immediately;
3. Stop
-(void) invalidate;
This is the only way to move the timer out of the Runloop.
4, in multi-threaded development, if the use of timers in the Mainthread, the two initialization methods can be used, if the use of timers in the sub-thread, can only use the method:
+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;
and start the timer can not use fire, only let Runloop continue to execute, sample code:
_timer =[nstimertimerwithtimeinterval:0.5target: Self selector:@selector(timeschedule)userInfo:Nil repeats:YES];
nsrunloop *runloop=[nsrunloopcurrentrunloop];
[Runloop addtimer:_timerformode:nsdefaultrunloopmode];
while ([Runlooprunmode:nsdefaultrunloopmode beforedate: [ nsdate distantfuture]]);