PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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