UIControl+Blocks.m 700 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // UIControl+Blocks.m
  3. //
  4. // Created by AvdLee on 05/08/14.
  5. // Copyright (c) 2014 A.Lee. All rights reserved.
  6. //
  7. #import "UIControl+Blocks.h"
  8. static char UIButtonHandlerKey;
  9. @implementation UIControl (Blocks)
  10. - (void)addEventHandler:(ActionBlock)handler forControlEvents:(UIControlEvents)controlEvents
  11. {
  12. objc_setAssociatedObject(self, &UIButtonHandlerKey, handler, OBJC_ASSOCIATION_COPY_NONATOMIC);
  13. [self addTarget:self action:@selector(callActionHandler:) forControlEvents:controlEvents];
  14. }
  15. - (void)callActionHandler:(id)sender
  16. {
  17. ActionBlock handler = (ActionBlock)objc_getAssociatedObject(self, &UIButtonHandlerKey);
  18. if (handler) {
  19. handler(sender);
  20. }
  21. }
  22. @end