NSTimer disables dealloc in UIView

后端 未结 3 1992
日久生厌
日久生厌 2021-02-06 02:56
@interface someview:UIView{
  NSTimer* timer;
}
@end

@implementation someview

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


        
3条回答
  •  别跟我提以往
    2021-02-06 03:26

    I think the best solution when using an NSTimer inside of a UIView is to override the removeFromSuperview method;

    - (void)removeFromSuperview
    {
        [timer invalidate];
        timer = nil;
    
        [super removeFromSuperview];
    }
    

    The only thing to keep in mind here is that you need to ensure that timer is not a nil object because removeFromSuperview can also get automatically called from other UIView's super dealloc methods. You could wrap in a conditional to check.

提交回复
热议问题