在當前控制器(ViewController)的view上添加了一個自定義的view(LXFTimerView), LXFTimerView在成功建立出來後添加了定時器NSTimer並加入RunLoop開始工做, 當在當前控制器裏將LXFTimerView移除掉後,定時器還在工做,並且LXFTimerView裏的dealloc並無調用微信
LXFTimerView.moop
#import "LXFTimerView.h"
@interface LXFTimerView()
/** 定時器 */
@property(nonatomic, weak) NSTimer *timer;
@end
@implementation LXFTimerView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self addTimer];
}
return self;
}
- (void)dealloc {
NSLog(@"LXFTimerView - dealloc");
[self removeTimer];
}
#pragma mark - 定時器方法
/** 添加定時器方法 */
- (void)addTimer {
// 建立定時器
if (self.timer) { return; }
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(log) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
/** 移除定時器 */
- (void)removeTimer {
[self.timer invalidate];
self.timer = nil;
}
- (void)log {
NSLog(@"定時器 -- %s", __func__);
}
@end
複製代碼
ViewController.mui
#import "ViewController.h"
#import "LXFTimerView.h"
@interface ViewController ()
/** timerView */
@property(nonatomic, weak) LXFTimerView *timerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LXFTimerView *timerView = [[LXFTimerView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
timerView.backgroundColor = [UIColor orangeColor];
self.timerView = timerView;
[self.view addSubview:timerView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.timerView removeFromSuperview];
}
@end
複製代碼
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:];
複製代碼
會將LXFTimerView進行強引用,什麼?我怎麼知道?看下圖 atom
翻譯:定時器保持着對target的強引用,直到定時器做廢 那爲何LXFTimerView中的timer屬性要用weak?? 不用着急,下面即將揭曉~讓定時器指着另外一個對象,讓那個對象來執行LXFTimerView中須要執行的方法。 引用關係以下圖所示 spa
建立一個繼承於NSObject的類 LXFWeakTarget,並提供一個建立定時器的方法(蘋果官方的方法,對scheduledTimerWithTimeInterval進行轉到定義操做【就是command+左鍵】就能夠獲得) LXFWeakTarget.h#import <Foundation/Foundation.h>
@interface LXFWeakTarget : NSObject
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
@end
複製代碼
#import "LXFWeakTarget.h"
@interface LXFWeakTarget()
@property(nonatomic, weak) id target;
@property(nonatomic, assign) SEL selector;
@end
@implementation LXFWeakTarget
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo {
// 建立當前類的對象
LXFWeakTarget *object = [[LXFWeakTarget alloc] init];
object.target = aTarget;
object.selector = aSelector;
return [NSTimer scheduledTimerWithTimeInterval:ti target:object selector:@selector(execute:) userInfo:userInfo repeats:yesOrNo];
}
- (void)execute:(id)obj {
[self.target performSelector:self.selector withObject:obj];
}
@end
複製代碼
在LXFTimerView.m中導入LXFWeakTarget的頭文件翻譯
#import "LXFWeakTarget.h"
複製代碼
將建立定時器的類改成 LXFWeakTarget3d
self.timer = [LXFWeakTarget scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(log) userInfo:nil repeats:YES];
複製代碼
如今再來執行一下程序 code
好,那「爲何LXFTimerView中的timer屬性要用weak」這個問題就不用多加解析了吧。orm