我这样添加定时器
tim=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatTim) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:tim forMode:NSDefaultRunLoopMode];
tim 它是我类级的 NSTimer 属性。
然后我在点击按钮时停止它
[[fbt tim] invalidate];
[fbt setTim:nil];
fbt 它是我的类的实例。
如果我只调用 invalidate 那么它不会停止,但是如果我将它设置为 nil 那么我得到 EXC_BREAKPOINT
这里是选择器中repeatTim方法的代码
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
[appDelegate.wbv stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"intal()"]];
我尝试在
中调用 init 和 invalidatedispatch_async(dispatch_get_main_queue(), ^{})
它也不会停止计时器。
最佳答案
您有多个计时器在运行。试试这个:
-(void)startTimer{
[self.myTimer invalidate]; // kill old timer
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(doSomething) userInfo:nil repeats:YES];
}
-(void)stopTimer{
[self.myTimer invalidate];
self.myTimer=nil; //set pointer to nil
}
关于ios - NSTimer 不会因无效而停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745175/