Created
April 18, 2014 10:52
-
-
Save libdx/11037460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ActionSheetItem : NSObject | |
@property (copy, nonatomic) NSString *title; | |
@property (nonatomic, getter=isDestructive) BOOL destructive; | |
@property (nonatomic, getter=isCancel) BOOL cancel; | |
@property (copy, nonatomic) void (^actionBlock)(); | |
@property (nonatomic) SEL action; | |
@property (weak, nonatomic) id target; | |
- (instancetype)initWithTitle:(NSString *)title target:(id)target action:(SEL)action; | |
- (instancetype)initWithTitle:(NSString *)title actionBlock:(void (^)())actionBlock; | |
@end | |
@interface DestructionActionSheetItem : ActionSheetItem | |
@end | |
@interface CancelActionSheetItem : ActionSheetItem | |
@end | |
@interface ActionSheetController : NSObject | |
@property (strong, nonatomic, readonly) UIActionSheet *actionSheet; | |
@property (copy, nonatomic) NSString *title; | |
@property (copy, nonatomic) NSArray *items; // array of ActionSheetItem's | |
@end | |
@interface ActionSheetItem () | |
@property (nonatomic) NSInteger index; | |
@end | |
@implementation ActionSheetItem | |
- (instancetype)initWithTitle:(NSString *)title target:(id)target action:(SEL)action actionBlock:(void (^)())actionBlock | |
{ | |
self = [super init]; | |
if (nil == self) | |
return nil; | |
self.title = title; | |
self.target = target; | |
self.action = action; | |
self.actionBlock = actionBlock; | |
self.index = -1; | |
return self; | |
} | |
- (instancetype)initWithTitle:(NSString *)title target:(id)target action:(SEL)action | |
{ | |
return [self initWithTitle:title target:target action:action actionBlock:nil]; | |
} | |
- (instancetype)initWithTitle:(NSString *)title actionBlock:(void (^)())actionBlock | |
{ | |
return [self initWithTitle:title target:nil action:NULL actionBlock:actionBlock]; | |
} | |
@end | |
@implementation DestructionActionSheetItem | |
- (BOOL)isDestructive | |
{ | |
return YES; | |
} | |
@end | |
@implementation CancelActionSheetItem | |
- (BOOL)isCancel | |
{ | |
return YES; | |
} | |
@end | |
@interface ActionSheetItem (ForActionSheetControllerEyes) | |
@property (nonatomic) NSInteger index; | |
@end | |
@interface ActionSheetController () <UIActionSheetDelegate> | |
@end | |
@implementation ActionSheetController | |
@synthesize actionSheet = _actionSheet; | |
- (UIActionSheet *)actionSheet | |
{ | |
if (nil == _actionSheet) { | |
_actionSheet = [[UIActionSheet alloc] init]; | |
_actionSheet.delegate = self; | |
_actionSheet.title = self.title; | |
for (ActionSheetItem *item in self.items) { | |
item.index = [_actionSheet addButtonWithTitle:item.title]; | |
if (item.isCancel) | |
_actionSheet.cancelButtonIndex = item.index; | |
if (item.isDestructive) | |
_actionSheet.destructiveButtonIndex = item.index; | |
} | |
} | |
return _actionSheet; | |
} | |
- (void)setItems:(NSArray *)items | |
{ | |
if (_items != items) { | |
_items = [items copy]; | |
_actionSheet = nil; | |
} | |
} | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"index = %d", buttonIndex]; | |
ActionSheetItem *item = [_items filteredArrayUsingPredicate:predicate].lastObject; | |
if (item.actionBlock) | |
item.actionBlock(); | |
else if (item.target && item.action) | |
[[UIApplication sharedApplication] sendAction:item.action to:item.target from:self forEvent:nil]; | |
} | |
// ... | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment