原文 http://www.jianshu.com/p/78efcc9575b7
利用Runtime实现给UIViewController添加一个分类,重写load
方法,在load方法中让viewWillAppear:
和已有方法(自己实现)例如:logViewWillAppear:
进行交换.
+ (void) load {
#ifdef DEBUG
//原本的viewWillAppear方法
Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
//需要替换成 能够输出日志的viewWillAppear
Method logViewWillAppear = class_getInstanceMethod(self, @selector(logViewWillAppear:));
//两方法进行交换
method_exchangeImplementations(viewWillAppear, logViewWillAppear);
#endif
}
- (void)logViewWillAppear:(BOOL)animated {
NSString *className = NSStringFromClass([self class]);
//在这里,你可以进行过滤操作,指定哪些viewController需要打印,哪些不需要打印
if ([className hasPrefix:@"UI"] == NO) {
NSLog(@"%@ will appear",className);
}
//下面方法的调用,其实是调用viewWillAppear
[self logViewWillAppear:animated];
}
网友评论