Created
January 23, 2014 10:33
-
-
Save atkit/8576368 to your computer and use it in GitHub Desktop.
Alert Builder
- how to use (pseudo code) - Alert.h
- Alert.m
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
// pseudo code | |
- (void) showSimpleAlert | |
{ | |
[[Alert alertWithTitle:@"Some Title" | |
message:@"Some Message"]show]; | |
} | |
- (void) showAlertWithYesNo | |
{ | |
[Alert alertWithTitle:@"Some Title" | |
message:@"Some Message" withYesBlock:^{ | |
// call method for YES | |
} withNoBlock:^{ | |
// call method for NO | |
}]; | |
} | |
- (void) showAlertWithOtherButtons | |
{ | |
Alert * alertBuilder = [Alert alertWithTitle:@"Delete item" | |
message:@"Are you sure you want to remove item"]; | |
[alertBuilder addButton:@"MayBe"]; // no block == no call | |
[alertBuilder addButton:@"Yeaa" withBlock:^{ | |
// call method for "Yeaa" | |
}]; | |
[alertBuilder show]; | |
} |
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
// | |
// AlertBuilder | |
// | |
#import <Foundation/Foundation.h> | |
typedef void (^AlertButtonBlock) (); | |
@interface Alert : NSObject <UIAlertViewDelegate> | |
@property (nonatomic, retain) NSString * title; | |
@property (nonatomic, retain) NSString * message; | |
+ (id) alert; | |
+ (id) alertWithTitle:(NSString*)title | |
message:(NSString*)message; | |
+ (id) alertWithTitle:(NSString*)title | |
message:(NSString*)message | |
withYesBlock:(AlertButtonBlock)yesBlock | |
withNoBlock:(AlertButtonBlock)noBlock; | |
- (id) withTitle: (NSString*)title; | |
- (id) withMessage:(NSString*)message; | |
- (id) addButton:(NSString*)title withBlock:(AlertButtonBlock)block; | |
- (id) addButton:(NSString*)title; | |
- (void) show; | |
@end |
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
// | |
// AlertBuilder | |
// | |
#import "Alert.h" | |
#import <objc/runtime.h> | |
@interface AlertButton : NSObject | |
@property (nonatomic, retain) NSString * title; | |
@property (nonatomic, copy) AlertButtonBlock block; | |
+ (id) alertButtonWithTitle:(NSString*)_title | |
block:(AlertButtonBlock)_block; | |
- (void) call; | |
@end | |
static char AlertAssociateObjectKey; | |
@implementation Alert { | |
NSMutableArray * alertButtons; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
alertButtons = [NSMutableArray new]; | |
} | |
return self; | |
} | |
- (UIAlertView *)buildAlertView | |
{ | |
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:self.title | |
message:self.message | |
delegate:self | |
cancelButtonTitle:nil | |
otherButtonTitles:nil]; | |
[self addOtherButtonTitlesToAlertView:alertView]; | |
[self addSelfToActivityView:alertView]; | |
return alertView; | |
} | |
- (void)addOtherButtonTitlesToAlertView:(UIAlertView *)alertView | |
{ | |
for (AlertButton * alertButton in alertButtons) | |
{ | |
[alertView addButtonWithTitle:alertButton.title]; | |
} | |
} | |
- (void)addSelfToActivityView:(UIAlertView *)alertView | |
{ | |
objc_setAssociatedObject(alertView, &AlertAssociateObjectKey, self, OBJC_ASSOCIATION_RETAIN); | |
} | |
- (void) show | |
{ | |
[[self buildAlertView] show]; | |
} | |
+ (id) alert | |
{ | |
return [Alert new]; | |
} | |
+ (id) alertWithTitle:(NSString*)title message:(NSString*)message | |
{ | |
return [[[self alert] withTitle:title] | |
withMessage:message]; | |
} | |
+ (id) alertWithTitle:(NSString*)title | |
message:(NSString*)message | |
withYesBlock:(AlertButtonBlock)yesBlock | |
withNoBlock:(AlertButtonBlock)noBlock | |
{ | |
Alert * alert = [Alert alertWithTitle:title | |
message:message]; | |
[alert addButton:NSLocalizedString(@"Yes", @"Alert - YES") withBlock:yesBlock]; | |
[alert addButton:NSLocalizedString(@"No" , @"Alert - NO") withBlock: noBlock]; | |
return alert; | |
} | |
- (id) withTitle:(NSString*)title | |
{ | |
self.title = title; | |
return self; | |
} | |
- (id) withMessage:(NSString *)message | |
{ | |
self.message = message; | |
return self; | |
} | |
- (id) addButton:(NSString*)title withBlock:(AlertButtonBlock)block | |
{ | |
[alertButtons addObject:[AlertButton alertButtonWithTitle:title | |
block:block]]; | |
return self; | |
} | |
- (id) addButton:(NSString*)title | |
{ | |
return [self addButton:title | |
withBlock:NULL]; | |
} | |
#pragma mark - UIAlertViewDelegate | |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
[alertButtons[buttonIndex] call]; | |
} | |
@end | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
@implementation AlertButton | |
+ (id)alertButtonWithTitle:(NSString*)_title | |
block:(AlertButtonBlock)_block | |
{ | |
AlertButton * alertButton = [AlertButton new]; | |
alertButton.title = _title; | |
alertButton.block = _block; | |
return alertButton; | |
} | |
- (void) call | |
{ | |
if (self.block) { | |
self.block(); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment