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:

  1. how stop timer if needed?
  2. 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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -