objective c - NSTimer stops when user goes to different view controller -


i have nstimer activated when user presses uibutton. leads method runs timer. timer runs when app running in background uiview animation stops when app pressed again , timer keeps running. whenever go uiviewcontroller timer stops , uilabels revert state if timer isn't running. how can fix timer still running when user comes original view controller timer.

- (ibaction)start:(id)sender {  //when timer stopped! if([startstop.titlelabel.text isequaltostring:@"stop"]){      nslog(@"button stopped");      stoptime = [nsdate date];      //nsstring log of time accquired     [timer invalidate];     timerisrunning = false;      //format stop time nsuserdefaults     nsdateformatter *savestoptime = [[nsdateformatter alloc] init];     [savestoptime setdateformat:@"hh:mm a"];     [savestoptime settimezone:[nstimezone localtimezone]];      nsstring *savestoptimestring = [savestoptime stringfromdate:stoptime];      //save stop time     nsuserdefaults *savestop = [nsuserdefaults standarduserdefaults];     [savestop setobject:savestoptimestring forkey:@"savedstoptime"];     [savestop synchronize];      timestoplabel.text = [nsstring stringwithformat:@"%@", savestoptimestring];      [startstop settitle:@"start" forstate:uicontrolstatenormal];      startstop.tintcolor = [uicolor colorwithred:0.19 green:0.803 blue:0.19 alpha:1]; // green      [uiview beginanimations:nil context:null];     [uiview setanimationduration:0.7];     [btnspin setalpha:0];     [uiview commitanimations]; } else { //when timer running      nslog(@"button started");      //start date recording when start     basedate = [nsdate date];     starttimedate = [nsdate date];     starttime = [nsdate date];      timer = [nstimer scheduledtimerwithtimeinterval:0.01 target:self selector:@selector(timeractivity) userinfo:nil repeats:yes];     timerisrunning = true;     valueinstoplabel = true;      if (timerisrunning == false) {         stoptime = [nsdate date];     }     if (valueinstoplabel == true) {         timestoplabel.text = @"--:-- --";     }     else if (valueinstoplabel == false) {         timestartlabel.text = @"--:-- --";     }      startstop.tintcolor = [uicolor redcolor];      [uiview beginanimations:nil context:null];     [uiview setanimationduration:0.8];     [btnspin setalpha:1];     btnspin.image = [uiimage imagenamed:nil];     [uiview commitanimations];      btnspin.image = [uiimage imagenamed:@"startspin.png"];     spin = [cabasicanimation animationwithkeypath:@"transform.rotation.z"];     spin.byvalue = [nsnumber numberwithfloat:((360*m_pi)/180)];     spin.duration = 2;     spin.repeatcount = maxfloat;     [btnspin.layer addanimation:spin forkey:@"red"]; } }  - (void)timeractivity {  valueinstoplabel = true;  nsdate *currentdate = [nsdate date]; nstimeinterval timeinterval = [currentdate timeintervalsincedate:self.basedate]; nsdate *timerdate = [nsdate datewithtimeintervalsince1970:timeinterval];  nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"hh:mm:ss"]; [dateformatter settimezone:[nstimezone timezoneforsecondsfromgmt:0.0]];  nsstring *timestring = [dateformatter stringfromdate:timerdate];  //save date nsuserdefaults *savedate = [nsuserdefaults standarduserdefaults]; [savedate setobject:timestring forkey:@"saveddate"]; [savedate synchronize];  //format start time date nsuserdefaults nsdateformatter *savestarttimedateformatter = [[nsdateformatter alloc] init]; [savestarttimedateformatter setdateformat:@"d/ll/yy"]; [savestarttimedateformatter settimezone:[nstimezone localtimezone]];  nsstring *savetimedatestring = [savestarttimedateformatter stringfromdate:starttimedate];  //save start time date nsuserdefaults *savetimedatestart = [nsuserdefaults standarduserdefaults]; [savetimedatestart setobject:savetimedatestring forkey:@"savedstartdatetime"]; [savetimedatestart synchronize];  timestartdatelabel.text = [nsstring stringwithformat:@"%@", savetimedatestring];  //format start time nsuserdefaults nsdateformatter *savestarttimeformatter = [[nsdateformatter alloc] init]; [savestarttimeformatter setdateformat:@"hh:mm a"]; [savestarttimeformatter settimezone:[nstimezone localtimezone]];  nsstring *savetimestring = [savestarttimeformatter stringfromdate:starttime];  //save start time nsuserdefaults *savetimestart = [nsuserdefaults standarduserdefaults]; [savetimestart setobject:savetimestring forkey:@"savedstarttime"]; [savetimestart synchronize];  timestartlabel.text = [nsstring stringwithformat:@"%@", savetimestring];  self.timelabel.text = timestring;  if (timerisrunning == true) {         [startstop settitle:@"stop" forstate:uicontrolstatenormal];     }  if (timerisrunning == false) {             [startstop settitle:@"start" forstate:uicontrolstatenormal];             [timer invalidate];             timer = nil;     stoptime = [nsdate date]; } } 


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -