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
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Using UIAlertView as the main example here, instead of calling the traditional `
The action blocks are of type RISimpleAction, which is typedef'd to be a block as follows:

typedef void (^RISimpleAction)();

This is just a simple block which takes no arguments and returns nothing.

The RIButtonItem class also provides a convenience method which returns an autoreleased item called, conveniently enough, `+item`. If you don't specify an action, the button will still show, but won't do anything when tapped other than dismiss the dialog. This is pretty common with cancel buttons, so another convenience method called `+itemWithLabel:` allows you to quickly create an item that has no action.
Expand All @@ -25,41 +25,46 @@ HOW TO USE IT

Typically, you'll create items that represent the buttons and the actions to take when they are tapped. For example imagine a dialog box confirming deletion of an item:

RIButtonItem *cancelItem = [RIButtonItem item];
cancelItem.label = @"No";
cancelItem.action = ^
{
RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"No" action:^{
// this is the code that will be executed when the user taps "No"
// this is optional... if you leave the action as nil, it won't do anything
// but here, I'm showing a block just to show that you can use one if you want to.
};
}];

RIButtonItem *deleteItem = [RIButtonItem item];
deleteItem.label = @"Yes";
deleteItem.action = ^
{
RIButtonItem *deleteItem = [RIButtonItem itemWithLabel:@"Yes" action:^{
// this is the code that will be executed when the user taps "Yes"
// delete the object in question...
[context deleteObject:theObject];
};
}];

The label property on the button items is the text that will be displayed in the button.

Once you've created these, you simply initialize your UIAlertView using the initializer, passing your button items accordingly:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Delete This Item?"
message:@"Are you sure you want to delete this really important thing?"
cancelButtonItem:cancelItem
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Delete This Item?"
message:@"Are you sure you want to delete this really important thing?"
cancelButtonItem:cancelItem
otherButtonItems:deleteItem, nil];
[alertView show];
[alertView release];

Alternatively you can also use a single line to create buttons and declare the alert view

[[[UIAlertView alloc] initWithTitle:@"Delete This Item?"
message:@"Are you sure you want to delete this really important thing?"
cancelButtonItem:[RIButtonItem itemWithLabel:@"Yes" action:^{
// Handle "Cancel"
}]
otherButtonItems:[RIButtonItem itemWithLabel:@"Delete" action:^{
// Handle "Delete"
}], nil] show];


Again, this is designed to be fire and forget, so you initialize it, show it, and release it. It'll take care of cleaning up after itself.

You can also add a RIButtonItem to the UIAlertView after initialization, just like you normally would:

[alertView addButtonItem:deleteItem];

This is useful if building an UIAlertView, or UIActionSheet dynamically from an Array:

for (RIButtonItem *item in buttonItemArray) {
Expand All @@ -83,12 +88,14 @@ copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
all copies or substantial portions of the Software source code. Permission
is hereby granted to reproduce the Software in binary form by compiling it
into your own projects without this attribution requirement.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
1 change: 1 addition & 0 deletions RIButtonItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
@property (copy, nonatomic) void (^action)();
+(id)item;
+(id)itemWithLabel:(NSString *)inLabel;
+(id)itemWithLabel:(NSString *)inLabel action:(void(^)(void))action;
@end

9 changes: 8 additions & 1 deletion RIButtonItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ +(id)item

+(id)itemWithLabel:(NSString *)inLabel
{
id newItem = [self item];
RIButtonItem *newItem = [self item];
[newItem setLabel:inLabel];
return newItem;
}

+(id)itemWithLabel:(NSString *)inLabel action:(void(^)(void))action
{
RIButtonItem *newItem = [self itemWithLabel:inLabel];
[newItem setAction:action];
return newItem;
}

@end

8 changes: 6 additions & 2 deletions UIActionSheet+Blocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ -(id)initWithTitle:(NSString *)inTitle cancelButtonItem:(RIButtonItem *)inCancel
}

- (NSInteger)addButtonItem:(RIButtonItem *)item
{
NSMutableArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);
{
if (nil == item) {
return -1;
}

NSMutableArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);

NSInteger buttonIndex = [self addButtonWithTitle:item.label];
[buttonsArray addObject:item];
Expand Down
8 changes: 6 additions & 2 deletions UIAlertView+Blocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ -(id)initWithTitle:(NSString *)inTitle message:(NSString *)inMessage cancelButto
}

- (NSInteger)addButtonItem:(RIButtonItem *)item
{
NSMutableArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);
{
if (nil == item) {
return -1;
}

NSMutableArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);

NSInteger buttonIndex = [self addButtonWithTitle:item.label];
[buttonsArray addObject:item];
Expand Down