我在哪里正确启动并使NSTimer无效?

时间:2014-04-12 10:12:20

标签: ios nstimer

在xcode中,当我记录我的NSTimer时,它从viewDidAppear开始并在viewdiddisappear停止,这很好,但是当我进入退出时,计时器恢复(我不会&#39} ; t想要发生,因为它加倍了。)我如何正确地阻止它,以便在再次调用时不会加倍?

..当我加入viewdidload时。确定它没有加倍,但它不会重新开始,因为我使它失效viewDidDisappear这里是我的代码

-(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];
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个,过去我在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;
}