diff --git a/README.md b/README.md index 8df978a..b326716 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ README ====== -This is a quickie pair of categories on UIAlertView and UIActionSheet which enables you to use blocks to handle the button selection instead of implementing a delegate. +This is a quickie set of categories on UIAlertView, UIActionSheet, and UIBarButtonItem which enables you to use blocks to handle the button selection instead of implementing a delegate. HOW IT WORKS ------------ @@ -75,6 +75,11 @@ That's it! The UIActionSheet category works virtually the same as the UIAlertView. Just check out the header for the initializer you need to use. It's very straightforward. +The UIBarButtonItem category adds a new initializer taking an RIButtonItem: + + [[UIBarButtonItem alloc] initWithStyle:UIBarButtonItemStyleBordered + item:buttonItem]; + FIND THIS USEFUL? ----------------- diff --git a/UIBarButtonItem+Blocks.h b/UIBarButtonItem+Blocks.h new file mode 100644 index 0000000..d272b62 --- /dev/null +++ b/UIBarButtonItem+Blocks.h @@ -0,0 +1,17 @@ +// +// UIBarButtonItem+Blocks.h +// Shibui +// +// Created by Kevin Vance on 8/28/14. +// + +#import +#import "RIButtonItem.h" + +@interface UIBarButtonItem (Blocks) + +- (id)initWithStyle:(UIBarButtonItemStyle)style item:(RIButtonItem *)item; + +- (void)setButtonItem:(RIButtonItem *)item; + +@end diff --git a/UIBarButtonItem+Blocks.m b/UIBarButtonItem+Blocks.m new file mode 100644 index 0000000..a0ee960 --- /dev/null +++ b/UIBarButtonItem+Blocks.m @@ -0,0 +1,35 @@ +// +// UIBarButtonItem+Blocks.m +// Shibui +// +// Created by Kevin Vance on 8/28/14. +// + +#import "UIBarButtonItem+Blocks.h" +#import + +static NSString *RI_BUTTON_ASS_KEY = @"com.random-ideas.BUTTON"; + +@implementation UIBarButtonItem (Blocks) + +- (id)initWithStyle:(UIBarButtonItemStyle)style item:(RIButtonItem *)item +{ + if((self = [self initWithTitle:item.label style:style target:self action:@selector(buttonItemPressed:)])) + { + [self setButtonItem:item]; + } + return self; +} + +- (void)buttonItemPressed:(id)sender { + RIButtonItem *item = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY); + if(item && item.action) { + item.action(); + } +} + +- (void)setButtonItem:(RIButtonItem *)item { + objc_setAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +@end