可能的原因是多次调用了scheduledTimerWithTimeInterval及[[NSRunLoopcurrentRunLoop]addTimer方法。
@property (nonatomic,assign)int time;
@property (nonatomic,assign)NSTimer *timer;
if ([_timer isValid]) {
[_timer invalidate];
}
_timer =nil;
self.time = (int)sender.value*60;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollTimer:) userInfo:nil repeats:YES];
[[NSRunLoopcurrentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
-(void)scrollTimer:(NSTimer *)theTimer{
self.time =self.time-1;
int minutes =self.time /60;
int seconds =self.time - minutes *60;
if (self.time <0) {
_timerLabel.text = [NSStringstringWithFormat:@"定时:"];
[self changeSpeech:_stopBtn];
return;
}
_timerLabel.text = [NSStringstringWithFormat:@"%d:%d",minutes,seconds];
NSLog(@"%@",_timerLabel.text);
}
2.使用完毕后及时销毁
-(void)changeSpeech:(UIButton *)sender
{
if ([_timer isValid]) {
[_timer invalidate];
}
_timer =nil;
}
本文介绍如何正确管理和使用NSTimer来避免iOS应用中的内存泄漏问题。通过确保每次只运行一个NSTimer实例,并在不再需要时及时销毁它,可以有效防止因重复创建定时器而导致的问题。

1039

被折叠的 条评论
为什么被折叠?



