objective c - Start & Stop Timer -
i have timer calls function every 10 seconds:
[nstimer scheduledtimerwithtimeinterval:10.0 target:self selector:@selector(checkformessages) userinfo:nil repeats:yes]; it calls function:
- (void)checkformessages { //do here } two questions:
- how stop timer if needed?
- can put timer , function somewhere can call different view controllers?
"scheduledtimerwithtimeinterval" returns "nstimer" object.
if hold onto object (e.g. set , via "property"), can stop via nstimer invalidate method.
and since you're asking code, add view controller's .h @interface:
@property (strong) nstimer * messagetimer; then, in view controller's .m file:
self.messagetimer = [nstimer scheduledtimerwithtimeinterval:10.0 target:self selector:@selector(checkformessages) userinfo:nil repeats:yes]; makes sense?
Comments
Post a Comment