Guest User

Untitled

a guest
Jun 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. + (void)load {
  2. static dispatch_once_t onceToken;
  3. dispatch_once(&onceToken, ^{
  4. SEL systemSEL = @selector(sendAction:to:forEvent:);
  5. SEL replaceSEL = @selector(SG_sendAction:to:forEvent:);
  6. Method systemMethod = class_getInstanceMethod(self, systemSEL);
  7. Method replaceMethod = class_getInstanceMethod(self, replaceSEL);
  8.  
  9. BOOL isAdd = class_addMethod(self, systemSEL, method_getImplementation(replaceMethod), method_getTypeEncoding(replaceMethod));
  10.  
  11. if (isAdd) {
  12. class_replaceMethod(self, replaceSEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
  13. } else {
  14. // 添加失败,说明本类中有 replaceMethod 的实现,此时只需要将 systemMethod 和 replaceMethod 的IMP互换一下即可
  15. method_exchangeImplementations(systemMethod, replaceMethod);
  16. }
  17. });
  18. }
Add Comment
Please, Sign In to add comment