用runTime的方法交换打印当前页面对应的控制器名称,方便修改bug的时候快速定位到APP当前页面对应的控制器。
1、第一步为所有控制器的父类UIViewController,创建一个分类UIViewController+LogViewController.h
2、第二部在load方法里交换viewWillAppear方法,实现自己的logViewWillAppear方法,获取当前控制器名称,并打印,具体实现如下:
#import "UIViewController+LogViewController.h"
#import
@implementationUIViewController (LogViewController)
+ (void)load
{
//为了修改bug的时候,方便找到界面对应的视图控制器
MethodviewWillAppear =class_getInstanceMethod(self,@selector(viewWillAppear:));
MethodlogViewWillAppear =class_getInstanceMethod(self,@selector(logViewWillAppear:));
method_exchangeImplementations(viewWillAppear, logViewWillAppear);
}
- (void)logViewWillAppear:(BOOL)animated {
NSString*className =NSStringFromClass([selfclass]);
if([classNamehasPrefix:@"UI"] ==NO) {
NSLog(@"界面对应的视图控制器是%@",className);
}
[self logViewWillAppear:animated];
}