NSTimer 在 UIView 中禁用 dealloc [英] NSTimer disables dealloc in UIView

查看:53
本文介绍了NSTimer 在 UIView 中禁用 dealloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@interface someview:UIView{
  NSTimer* timer;
}
@end

@implementation someview

-(void)dealloc{
  NSLog(@"dealloc someview");
  [timer invalidate];
  timer = nil;
}
-(void)runTimer{
//
}
-(void)someMethod{

  timer = [NSTimer timerWithTimeInterval:2.0f target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
}

@end

释放 someview 不会调用 dealloc 并且计时器会继续运行.

Releasing someview will NOT call dealloc and the timer keeps on running.

如果我注释掉timer = [NSTimer schedule ...."部分,dealloc 将被调用.这意味着我的代码的所有其他部分都正常工作,而计时器是罪魁祸首.runTimer 方法是空的,这意味着它只是计时器在搞乱我.

If I comment out the "timer = [NSTimer schedule...." part, dealloc will be called. Which means all the other part of my code is working properly and the timer is the culprit. The runTimer method is empty, which means it's just the timer messing with me.

推荐答案

NSTimer 保留目标.因此,在您的视图被释放之前,计时器必须失效.

NSTimer retains the target. Therefore, the timer must be invalidated before your view is dealloc'd.

这篇关于NSTimer 在 UIView 中禁用 dealloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆