问题是这样的:
我需要能够在具有大量 tableViews 的大型现有应用程序中对 didSelectRowAtIndexPath 进行分析。
我的第一个想法是在 didSelectRowAtIndexPath 上进行方法调配:但是我的应用程序崩溃并显示“无法识别的选择器发送到实例”消息,具体取决于在原始 didSelectRowAtIndexPath 实现中访问的内容。
这是我尝试在 UIViewController 类别中实现此目的的方法:
#import "UIViewController+Swizzle.h"
@implementation UIViewController (Swizzle)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPathSwizzled:(NSIndexPath *)indexPath {
NSLog(@"Log something here");
[self tableView:tableView didSelectRowAtIndexPathSwizzled:indexPath];
}
+ (void) initialize {
BOOL conformsToTableViewDelegate = class_conformsToProtocol(self, @protocol(UITableViewDelegate));
if(conformsToTableViewDelegate) {
Method method1 = class_getInstanceMethod(self, @selector(tableView:didSelectRowAtIndexPath:));
Method method2 = class_getInstanceMethod(self, @selector(tableView:didSelectRowAtIndexPathSwizzled:));
method_exchangeImplementations(method1, method2);
}
}
@end
这可以实现吗?如果是这样,我做错了什么?
谢谢!