Skip to content

Instantly share code, notes, and snippets.

@YordiLorenzo
Last active March 5, 2017 00:15
Show Gist options
  • Save YordiLorenzo/d244e2aec86b26d4dabb to your computer and use it in GitHub Desktop.
Save YordiLorenzo/d244e2aec86b26d4dabb to your computer and use it in GitHub Desktop.
UIAlertController extension
#import "UIAlertController+DKExtensions.h"
@implementation UIAlertController (DKExtensions)
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
+ (UIAlertController *)alertWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:cancelBlock];
[alertController addAction:cancelAction];
return alertController;
}
+ (UIAlertController *)alertWithTitle:(NSString *)title message:(NSString *)message, ...; {
va_list arguments;
va_start(arguments, message);
NSString *fullMessage = [[NSString alloc] initWithFormat:message arguments:arguments];
va_end(arguments);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:fullMessage preferredStyle:UIAlertControllerStyleAlert];
return alertController;
}
+ (UIAlertController *)alertWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock
otherButtonTitlesAndBlocks:(NSString *)firstTitle, ...; {
UIAlertController *alertController = [UIAlertController alertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock];
va_list arguments;
va_start(arguments, firstTitle);
NSString *string = firstTitle;
dispatch_block_t block;
while(string)
{
block = va_arg(arguments, dispatch_block_t);
UIAlertAction *action = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:block ? ^(UIAlertAction *action) {
block();
} : nil];
[alertController addAction:action];
string = va_arg(arguments, id);
}
va_end(arguments);
return alertController;
}
/**
Alert with text input
**/
+ (UIAlertController *)textInputWithConfigurationHandler:(void (^)(UITextField *textfield))handler title:(NSString *)title message:(NSString *)message
confirmButtonTitle:(NSString *)confirmButtonTitle confirmBlock:(void (^)())confirmBlock
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController alertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock otherButtonTitlesAndBlocks:confirmButtonTitle, confirmBlock, nil];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
handler(textField);
}];
return alertController;
}
+ (UIAlertController *)textInputWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle confirmButtonTitle:(NSString *)confirmButtonTitle confirmBlock:(void (^)())confirmBlock cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController alertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock otherButtonTitlesAndBlocks:confirmButtonTitle,confirmBlock, nil];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"Input";
textField.textColor = [UIColor blackColor];
}];
return alertController;
}
+ (UIAlertController *)secureTextInputWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle confirmButtonTitle:(NSString *)confirmButtonTitle confirmBlock:(void (^)())confirmBlock cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController alertWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock otherButtonTitlesAndBlocks:confirmButtonTitle,confirmBlock, nil];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.secureTextEntry = YES;
}];
return alertController;
}
+ (UIAlertController *)loginInputWithTitle:(NSString *)title message:(NSString *)message
confirmButtonTitle:(NSString *)confirmButtonTitle
confirmBlock:(void (^)())confirmBlock
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController textInputWithConfigurationHandler:^(UITextField *textfield) {
textfield.placeholder = @"Username";
} title:title message:message confirmButtonTitle:confirmButtonTitle confirmBlock:confirmBlock cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"Password";
textField.secureTextEntry = YES;
}];
return alertController;
}
/**
Action sheets
**/
+ (UIAlertController *)actionSheetWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:cancelBlock];
[alertController addAction:cancelAction];
return alertController;
}
+ (UIAlertController *)actionSheetWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock
otherButtonTitlesAndBlocks:(NSString *)firstTitle, ...; {
UIAlertController *alertController = [UIAlertController actionSheetWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock];
va_list arguments;
va_start(arguments, firstTitle);
NSString *string = firstTitle;
dispatch_block_t block;
while(string)
{
block = va_arg(arguments, dispatch_block_t);
UIAlertAction *action = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:block ? ^(UIAlertAction *action) {
block();
} : nil];
[alertController addAction:action];
string = va_arg(arguments, id);
}
va_end(arguments);
return alertController;
}
+ (UIAlertController *)actionSheetWithTitle:(NSString *)title message:(NSString *)message
destructiveTitle:(NSString *)destructiveTitle
destructiveBlock:(void (^)())destructiveBlock
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock; {
UIAlertController *alertController = [UIAlertController actionSheetWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock];
UIAlertAction *action = [UIAlertAction actionWithTitle:destructiveTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
destructiveBlock();
}];
[alertController addAction:action];
return alertController;
}
+ (UIAlertController *)actionSheetWithTitle:(NSString *)title message:(NSString *)message
destructiveTitle:(NSString *)destructiveTitle
destructiveBlock:(void (^)())destructiveBlock
cancelButtonTitle:(NSString *)cancelButtonTitle cancelBlock:(void (^)())cancelBlock
otherButtonTitlesAndBlocks:(NSString *)firstTitle, ... NS_REQUIRES_NIL_TERMINATION; {
UIAlertController *alertController = [UIAlertController actionSheetWithTitle:title message:message cancelButtonTitle:cancelButtonTitle cancelBlock:cancelBlock];
UIAlertAction *action = [UIAlertAction actionWithTitle:destructiveTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
destructiveBlock();
}];
[alertController addAction:action];
va_list arguments;
va_start(arguments, firstTitle);
NSString *string = firstTitle;
dispatch_block_t block;
while(string)
{
block = va_arg(arguments, dispatch_block_t);
UIAlertAction *action = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:block ? ^(UIAlertAction *action) {
block();
} : nil];
[alertController addAction:action];
string = va_arg(arguments, id);
}
va_end(arguments);
return alertController;
}
- (void) dismissAfter:(NSTimeInterval)interval completionBlock:(void (^)())completionBlock; {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, interval * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:completionBlock];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment