在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];
}
答案 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;
}