Last active
August 29, 2015 14:07
-
-
Save onmyway133/a5b2de7e651ca11e8267 to your computer and use it in GitHub Desktop.
FTGActionSheetController
This file contains hidden or 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
// .h | |
#import <Foundation/Foundation.h> | |
typedef void (^FTGHandler)(); | |
@interface FTGActionSheetController : NSObject | |
- (instancetype)initWithTitle:(NSString *)title; | |
- (instancetype)initWithTitle:(NSString *)title | |
cancelButtonTitle:(NSString *)cancelButtonTitle | |
destructiveButtonTitle:(NSString *)destructiveButtonTitle; | |
- (void)addButtonWithTitle:(NSString *)buttonTitle handler:(FTGHandler)handler; | |
- (void)showInView:(UIView *)view; | |
@end | |
// .m | |
#import "FTGActionSheetController.h" | |
@interface FTGActionSheetController () <UIActionSheetDelegate> | |
@property (nonatomic, strong) UIActionSheet *actionSheet; | |
@property (nonatomic, strong) NSMutableDictionary *buttonToHandlerMapping; | |
@end | |
@implementation FTGActionSheetController | |
- (instancetype)initWithTitle:(NSString *)title | |
{ | |
self = [self initWithTitle:title message:message cancelButtonTitle:nil destructiveButtonTitle:nil]; | |
return self; | |
} | |
- (instancetype)initWithTitle:(NSString *)title | |
cancelButtonTitle:(NSString *)cancelButtonTitle | |
destructiveButtonTitle:(NSString *)destructiveButtonTitle | |
{ | |
self = [super init]; | |
if (self) { | |
_actionSheet = [[UIActionSheet alloc] initWithTitle:title | |
delegate:self | |
cancelButtonTitle:cancelButtonTitle | |
destructiveButtonTitle:destructiveButtonTitle | |
otherButtonTitles:nil]; | |
_buttonToHandlerMapping = [NSMutableDictionary dictionary]; | |
} | |
return self; | |
} | |
- (void)addButtonWithTitle:(NSString *)buttonTitle handler:(FTGHandler)handler | |
{ | |
[self.actionSheet addButtonWithTitle:buttonTitle]; | |
if (handler) { | |
[self.buttonToHandlerMapping setObject:[handler copy] forKey:buttonTitle]; | |
} | |
} | |
- (void)showInView:(UIView *)view | |
{ | |
self.actionSheet.cancelButtonIndex = self.actionSheet.numberOfButtons - 1; | |
[self.actionSheet showInView:view]; | |
} | |
#pragma mark - UIActionSheetDelegate | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; | |
FTGHandler handler = self.buttonToHandlerMapping[buttonTitle]; | |
if (handler) { | |
handler(); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment