From d3ffe3d89f126dce73ea682d8d8986c93f3d85e8 Mon Sep 17 00:00:00 2001 From: Lin Junjie Date: Wed, 21 Aug 2013 16:40:02 +0800 Subject: [PATCH] Adds initWithTitle: and addButtonWithTitle:type:action: to UIActionSheet+Blocks. Updated README.md to show its usage. --- README.md | 22 ++++++++++++++++++++++ UIActionSheet+Blocks.h | 17 +++++++++++++++++ UIActionSheet+Blocks.m | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 209e9fa..76c8dc4 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,28 @@ 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. +If you prefer not to create your `RIButton`s separately, you can initialize an empty action sheet with just a title using `initWithTitle:`, then add buttons to it with `addButtonWithTitle:type:action:` method, which also allows you to specify which button should be destructive and which should be cancel. + + UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Replace existing item?"]; + + [actionSheet addButtonWithTitle:@"Replace" + type:UIActionSheetButtonTypeDestructive + action:^{ + // Replace, mark as destructive + }]; + + [actionSheet addButtonWithTitle:@"Keep Both" + type:UIActionSheetButtonTypeNormal + action:^{ + // Keep both, mark as normal + }]; + + [actionSheet addButtonWithTitle:@"Cancel" + type:UIActionSheetButtonTypeCancel + action:^{ + // Don't replace, mark as cancel + }]; + LICENSE ------- diff --git a/UIActionSheet+Blocks.h b/UIActionSheet+Blocks.h index 2ecba7f..4e12632 100644 --- a/UIActionSheet+Blocks.h +++ b/UIActionSheet+Blocks.h @@ -9,12 +9,29 @@ #import #import "RIButtonItem.h" +/// Specifies if the button should is a destructive button or a cancel button +typedef enum : NSInteger { + UIActionSheetButtonTypeNormal, /*!< Normal button, no additional styling */ + UIActionSheetButtonTypeDestructive, /*!< Destructive button, styled in red + background pre-iOS 7, and in red + foreground color in iOS 7 */ + UIActionSheetButtonTypeCancel, /*!< Cancel button, styled with darker + background pre-iOS 7, and physically + separated from other buttons in iOS 7 */ +} UIActionSheetButtonType; + @interface UIActionSheet (Blocks) +-(id)initWithTitle:(NSString *)inTitle; + -(id)initWithTitle:(NSString *)inTitle cancelButtonItem:(RIButtonItem *)inCancelButtonItem destructiveButtonItem:(RIButtonItem *)inDestructiveItem otherButtonItems:(RIButtonItem *)inOtherButtonItems, ... NS_REQUIRES_NIL_TERMINATION; - (NSInteger)addButtonItem:(RIButtonItem *)item; +- (NSInteger)addButtonWithTitle:(NSString *)title + type:(UIActionSheetButtonType)buttonType + action:(void(^)(void))action; + /** This block is called when the action sheet is dismssed for any reason. */ @property (copy, nonatomic) void(^dismissalAction)(); diff --git a/UIActionSheet+Blocks.m b/UIActionSheet+Blocks.m index b852947..5707fa5 100644 --- a/UIActionSheet+Blocks.m +++ b/UIActionSheet+Blocks.m @@ -14,6 +14,14 @@ @implementation UIActionSheet (Blocks) +-(id)initWithTitle:(NSString *)inTitle +{ + return [self initWithTitle:inTitle + cancelButtonItem:nil + destructiveButtonItem:nil + otherButtonItems:nil]; +} + -(id)initWithTitle:(NSString *)inTitle cancelButtonItem:(RIButtonItem *)inCancelButtonItem destructiveButtonItem:(RIButtonItem *)inDestructiveItem otherButtonItems:(RIButtonItem *)inOtherButtonItems, ... { if((self = [self initWithTitle:inTitle delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil])) @@ -66,7 +74,32 @@ - (NSInteger)addButtonItem:(RIButtonItem *)item return buttonIndex; } - + +- (NSInteger)addButtonWithTitle:(NSString *)title + type:(UIActionSheetButtonType)buttonType + action:(void(^)(void))action +{ + RIButtonItem *button = + [RIButtonItem itemWithLabel:title action:action]; + + NSInteger index = [self addButtonItem:button]; + + switch (buttonType) { + case UIActionSheetButtonTypeCancel: + [self setCancelButtonIndex:index]; + break; + + case UIActionSheetButtonTypeDestructive: + [self setDestructiveButtonIndex:index]; + break; + + default: + break; + } + + return index; +} + - (void)setDismissalAction:(void(^)())dismissalAction { objc_setAssociatedObject(self, (__bridge const void *)RI_DISMISSAL_ACTION_KEY, nil, OBJC_ASSOCIATION_COPY);