/BlocksKit/UIBarButtonItem+BlocksKit.m

http://github.com/zwaldowski/BlocksKit · Objective C · 41 lines · 28 code · 9 blank · 4 comment · 1 complexity · 42d92889e10a9af74dd73fabb64a6c8b MD5 · raw file

  1. //
  2. // UIBarButtonItem+BlocksKit.m
  3. // BlocksKit
  4. //
  5. #import "UIBarButtonItem+BlocksKit.h"
  6. #import "NSObject+AssociatedObjects.h"
  7. static char kBarButtonItemBlockKey;
  8. @interface UIBarButtonItem (BlocksKitPrivate)
  9. - (void)_handleAction:(UIBarButtonItem *)sender;
  10. @end
  11. @implementation UIBarButtonItem (BlocksKit)
  12. - (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem handler:(BKSenderBlock)action {
  13. self = [self initWithBarButtonSystemItem:systemItem target:self action:@selector(_handleAction:)];
  14. [self associateCopyOfValue:action withKey:&kBarButtonItemBlockKey];
  15. return self;
  16. }
  17. - (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style handler:(BKSenderBlock)action {
  18. self = [self initWithImage:image style:style target:self action:@selector(_handleAction:)];
  19. [self associateCopyOfValue:action withKey:&kBarButtonItemBlockKey];
  20. return self;
  21. }
  22. - (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style handler:(BKSenderBlock)action {
  23. self = [self initWithTitle:title style:style target:self action:@selector(_handleAction:)];
  24. [self associateCopyOfValue:action withKey:&kBarButtonItemBlockKey];
  25. return self;
  26. }
  27. - (void)_handleAction:(UIBarButtonItem *)sender {
  28. BKSenderBlock block = [self associatedValueForKey:&kBarButtonItemBlockKey];
  29. if (block)
  30. block(self);
  31. }
  32. @end