标签:tin -o action exchange 定义 tool eve elf 运行
#import "UIButton+CH.h"
#import <objc/runtime.h>
@implementation UIButton (CH)
+ (void)load {
[super load];
// 这个是系统原有的方法
Method oldObjectAtIndex = class_getInstanceMethod([UIButton class], @selector(sendAction:to:forEvent:));
// 自定义的方法
Method newObjectAtIndex = class_getInstanceMethod([UIButton class], @selector(custom_sendAction:to:forEvent:));
// 交换方法
method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
}
- (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
// 可条件选择走系统方法
[self custom_sendAction:action to:target forEvent:event];
[CHTool playSoundEffect:@"music3" withType:VoiceTypeIsMp3];
CHLog(@"捕捉所有按钮事件");
}
@end
#import <objc/runtime.h>
@implementation NSMutableArray (LoggingAddObject)
+ (void)load {
Method addobject = class_getInstanceMethod(self, @selector(addObject:));
Method logAddobject = class_getInstanceMethod(self, @selector(logAddObject:));
method_exchangeImplementations(addObject, logAddObject);
}
- (void)logAddObject:(id)aobject {
[self logAddObject:aObject];
NSLog(@"Added object %@ to array %@", aObject, self);
}
@end
标签:tin -o action exchange 定义 tool eve elf 运行
原文地址:https://www.cnblogs.com/CH520/p/13794728.html