Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
------------
Expand Down Expand Up @@ -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?
-----------------

Expand Down
17 changes: 17 additions & 0 deletions UIBarButtonItem+Blocks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIBarButtonItem+Blocks.h
// Shibui
//
// Created by Kevin Vance on 8/28/14.
//

#import <UIKit/UIKit.h>
#import "RIButtonItem.h"

@interface UIBarButtonItem (Blocks)

- (id)initWithStyle:(UIBarButtonItemStyle)style item:(RIButtonItem *)item;

- (void)setButtonItem:(RIButtonItem *)item;

@end
35 changes: 35 additions & 0 deletions UIBarButtonItem+Blocks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// UIBarButtonItem+Blocks.m
// Shibui
//
// Created by Kevin Vance on 8/28/14.
//

#import "UIBarButtonItem+Blocks.h"
#import <objc/runtime.h>

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