咋整
找到一個(gè)只加載一次的方法+(void)load
將viewDidLoad
改成自定義的方法xya_viewDidLoad
+ (void)load{
[super load];
Method fromMethod = class_getInstanceMethod([self class], @selector(viewDidLoad));
Method toMethod = class_getInstanceMethod([self class], @selector(xya_viewDidLoad));
//判斷是否已經(jīng)添加了我們自定義的方法,若沒(méi)有邦投,就去添加
if (!class_addMethod([self class], @selector(viewDidLoad), method_getImplementation(toMethod), method_getTypeEncoding(toMethod))) {
method_exchangeImplementations(fromMethod, toMethod);
}
}
在自定義的xya_viewDidLoad
設(shè)置統(tǒng)計(jì)功能
- (void)xya_viewDidLoad{
NSString *str = [NSString stringWithFormat:@"%@", self.class];
// 我們?cè)谶@里加一個(gè)判斷伤锚,將系統(tǒng)的UIViewController的對(duì)象剔除掉
if(![str containsString:@"UI"]){
...你的統(tǒng)計(jì)代碼
}
[self xya_viewDidLoad];
}
附錄
IMP是”implementation”的縮寫(xiě),它是objetive-C 方法(method)實(shí)現(xiàn)代碼塊的地址志衣,可像C函數(shù)一樣直接調(diào)用屯援。通常情況下我們是通過(guò)[object method:parameter]或objc_msgSend()的方式向?qū)ο蟀l(fā)送消息,然后Objective-C運(yùn)行時(shí)(Objective-C runtime)尋找匹配此消息的IMP,然后調(diào)用它;但有些時(shí)候我們希望獲取到IMP進(jìn)行直接調(diào)用念脯。
根據(jù)SEL調(diào)用一個(gè)方法
SEL s1 = @selector(test1);
SEL s2= NSSelectorFromString(@"test1");
//轉(zhuǎn)化SEL為NSString:
// NSString *str = NSStringFromSelector(@selector(test));
//根據(jù)SEL來(lái)調(diào)用一個(gè)方法:
[self performSelector:s2];
NSLog(@"");