From 30c1284e082b0787951c83824bfa6af9f6ef0afe Mon Sep 17 00:00:00 2001 From: Kevin Vance Date: Thu, 28 Aug 2014 11:11:55 -0400 Subject: [PATCH 1/2] Add UIBarButtonItem support. The UIBarButtonItem+Blocks category adds an initializer for an RIButtonItem. A single RIButtonItem may be assigned to a UIBarButtonItem, unlike alerts and action sheets which support many. --- UIBarButtonItem+Blocks.h | 17 +++++++++++++++++ UIBarButtonItem+Blocks.m | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 UIBarButtonItem+Blocks.h create mode 100644 UIBarButtonItem+Blocks.m 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 From 00d3f274f2a0fe3b4e3c2a370039b902577b674f Mon Sep 17 00:00:00 2001 From: Kevin Vance Date: Thu, 28 Aug 2014 11:20:03 -0400 Subject: [PATCH 2/2] Update README with UIBarButtonItem info. --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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? -----------------