0

in xcode, when i log my NSTimer, It starts atviewDidAppear and stops on viewdiddisappear which is fine, but when I go to an exit segue, the timer resumes (which I don't want to happen because its getting doubled).how do I properly stop it so it won't get doubled when called again?

..when I put in on viewdidload.sure it doesn't get doubled, but it won't start again since I invalidate it viewDidDisappear here is my code

-(void)viewDidAppear:(BOOL)animated{
    if (self.timeInc.isValid == NO){

        self.timeInc = [NSTimer scheduledTimerWithTimeInterval:1.0f  target:self selector:@selector(updateDetailCounter:) userInfo:([Dateformatter2() dateFromString:_DetailModal[1]]) repeats:YES];


    }
}

-(void)viewDidDisappear:(BOOL)animated{
    [self.timeInc invalidate];
}
4
  • What do you mean at when I go to an exit segue?
    – Kumar KL
    Apr 12, 2014 at 10:21
  • it goes to this unwind command located on the root view of navigated pages -(IBAction)unwindToMain:(UIStoryboardSegue *)segue{ } Apr 12, 2014 at 10:25
  • 1
    Always call super if you override viewDidAppear:, viewDidDisappear:, ... If you unwind then the controller should destroy the timer before it gets destroyed - is your controller destroyed or artificially retained? When you say 'doubled', you mean you have additional timers running?
    – Wain
    Apr 12, 2014 at 10:36
  • no, i mean another one will be added once I open this view once more, then another one again and again and again..which may crash the app Apr 12, 2014 at 10:50

1 Answer 1

0

You can try this, in the past I had the same problem in viewDidAppear, viewDidDisappear

- (void) viewWillAppear:(BOOL)animated{
   [super viewWillAppear:animated];
   if (self.timerInc) {
    [self.timerInc invalidate];
    self.timerInc = nil;
   }
   _timerInc = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doSth) userInfo:nil repeats:YES];
}

- (void) viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  [self.timerInc invalidate];
  self.timerInc = nil;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.