iphone - Timer doesn't start in viewWillAppear Methods -
i want start timer when viewwillappear
method gets called.
it's working perfectly, i'm having problem when push new view on current view, has timer. when pop new view , recall viewwillappear
want start timer again before. have of techniques can't find solution.
-(void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [nsthread detachnewthreadselector:@selector(createtimer) totarget:self withobject:self]; } - (void)createtimer { nsautoreleasepool *pool = [[nsautoreleasepool alloc] init]; nsrunloop* runloop = [nsrunloop currentrunloop]; gametimer = [[nstimer timerwithtimeinterval:1.00 target:self selector:@selector(timerfired:) userinfo:nil repeats:yes] retain]; [[nsrunloop currentrunloop] addtimer:gametimer formode:nsdefaultrunloopmode]; timecount = 360; [runloop run]; [pool release]; } - (void)timerfired:(nstimer *)timer { if(timecount == 0) { [self timerexpired]; } else { timecount--; if(timecount == 0) { [timer invalidate]; [self timerexpired]; } } timelabel.text = [nsstring stringwithformat:@"%02d:%02d",timecount/60, timecount % 60]; } - (void) timerexpired { //nslog(@"final == %@",arrayanswers); //nslog(@"attempt == %d",[arrayanswers count]); [gametimer invalidate]; gametimer = nil; }
well, based on experience, problem timer's thread. basically, problem here:
[nsthread detachnewthreadselector:@selector(createtimer) totarget:self withobject:self];
i'm not absolutely sure believe code not wrong. haven't figured out yet guess nstimer object not being activated when detachnewthreadselector:
used. try executing timer on main thread.
[self performselectoronmainthread:@selector(createtimer) withobject:nil waituntildone:no]
i've done , got results wanted.
Comments
Post a Comment