Created
October 21, 2014 02:12
-
-
Save anonymous/17be26ff124bbeef722e 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
//UIAlertMode.h | |
#import <Foundation/Foundation.h> | |
#import <UIKit/UIKit.h> | |
typedef void (^AlertViewSelectionBlock) (NSInteger); | |
typedef void (^AlertControllerDefaultAction ) (UIAlertAction *); | |
typedef void (^AlertControllerCancelAction) (UIAlertAction *); | |
typedef void (^GenericBlock) (void *); | |
@interface UIAlertMode : NSObject | |
@property (assign, nonatomic) AlertViewSelectionBlock alertViewBlock; | |
@property (assign, nonatomic) AlertControllerCancelAction cancelAlertAction; | |
@property (assign, nonatomic) AlertControllerDefaultAction defaultAlertAction; | |
- (id) initAlertWith:(NSString *) title | |
:(NSString *) message | |
:(NSString *) cancelButtonTitle | |
:(NSString *) otherButtonTitle; | |
- (BOOL) supportsiOS8; | |
- (void) showAlertFor:(AlertViewSelectionBlock) alertViewBlock; | |
- (void) showAlertControllerFor:(AlertControllerDefaultAction) defaultAction :(AlertControllerCancelAction) cancelAction; | |
- (void) showAlertWith:(GenericBlock) block; | |
@end | |
// UIAlertMode.m | |
#import "UIAlertMode.h" | |
#import <objc/objc-runtime.h> | |
#import "AppDelegate.h" | |
@interface UIAlertMode () <UIAlertViewDelegate> { | |
UIAlertView *_alertView; | |
UIAlertController *_alertController; | |
} | |
@end | |
@implementation UIAlertMode | |
@synthesize alertViewBlock; | |
@synthesize defaultAlertAction; | |
@synthesize cancelAlertAction; | |
- (id) init { | |
self = [super init]; | |
return self; | |
} | |
- (id) initAlertWith:(NSString *) title | |
:(NSString *) message | |
:(NSString *) cancelButtonTitle | |
:(NSString *) otherButtonTitle { | |
self = [super init]; | |
if ([self supportsiOS8]) { | |
_alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; | |
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { | |
if (self.defaultAlertAction) { | |
self.defaultAlertAction(action); | |
} | |
}]; | |
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { | |
if (self.cancelAlertAction) { | |
self.cancelAlertAction(action); | |
} | |
}]; | |
[_alertController addAction:defaultAction]; | |
[_alertController addAction:cancelAction]; | |
} else { | |
_alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil]; | |
} | |
return self; | |
} | |
#pragma mark - Public API | |
- (BOOL) supportsiOS8 { | |
Class kAlertViewClass = NSClassFromString(@"UIAlertController"); | |
return !kAlertViewClass ? NO : YES; | |
} | |
- (void) showAlertFor:(AlertViewSelectionBlock) alertBlock { | |
self.alertViewBlock = alertBlock; | |
[_alertView show]; | |
} | |
- (void) showAlertControllerFor:(AlertControllerDefaultAction) defaultAction :(AlertControllerCancelAction) cancelAction { | |
self.cancelAlertAction = cancelAction; | |
self.defaultAlertAction = defaultAction; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; | |
[delegate.window.rootViewController presentViewController:_alertController animated:YES completion:nil]; | |
}); | |
} | |
#pragma mark - UIAlertView delegate method | |
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
if (self.alertViewBlock) { | |
self.alertViewBlock(buttonIndex); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment