Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds initWithTitle: and addButtonWithTitle:type:action: to UIActionSheetBlocks #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down
17 changes: 17 additions & 0 deletions UIActionSheet+Blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@
#import <UIKit/UIKit.h>
#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) <UIActionSheetDelegate>

-(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)();
Expand Down
35 changes: 34 additions & 1 deletion UIActionSheet+Blocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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);
Expand Down