问题 由于nstimer 和 视图 非常容易循环引用
1.这里提供一个一个很好的解决办法
#import <Foundation/Foundation.h>
@interface NSTimer (Support)
+ (NSTimer *)wx_scheduledTimerWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeat;
@end
#import "NSTimer+Support.h"
@implementation NSTimer (Support)
+ (NSTimer *)wx_scheduledTimerWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeat {
// 传入的 self 其实就是 NSTimer ,是一个类对象 (和实例对象区分开)
return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(support_blockInvoke:) userInfo:[block copy] repeats:YES];
}
+ (void)support_blockInvoke:(NSTimer *)timer {
void(^block)() = timer.userInfo;
if (block) {
block();
}
}
@end
//这是一个扩展类别
// 解释一下为什么这样可以
对于这个方法 target:self; 然后因为我们目标方法是support_blockInvoke:这个是一个加方法,显然这个self 就是 Class(不是实例就不会导致循环引用)
[self scheduledTimerWithTimeInterval:interval target:self selector:@selector(support_blockInvoke:) userInfo:[block copy] repeats:YES]