o9s
is a microblog
home archives search feed blogroll


UIAlertController With Text Input

// Very important: Text fields can only be added to alert controllers of style UIAlertControllerStyleAlert.
UIAlertController *pendingAlert =
    [UIAlertController
     alertControllerWithTitle:@"Title"
     message:@"Message"
     preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok =
    [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
     handler:^(UIAlertAction * action) {
         UITextField *textField = alert.textFields[0];
         NSLog(@"text was %@", textField.text);
     }];

UIAlertAction *cancel =
    [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
     handler:^(UIAlertAction * action) {
         [alert dismissViewControllerAnimated:YES completion:nil];
     }];

[alert addAction:ok];
[alert addAction:cancel];

[alert addTextFieldWithConfigurationHandler:nil];

[self presentViewController:alert animated:YES completion:nil];

The code above works but I like to encapsulate it in a class called TextFieldAlert.

TextFieldAlert.h

typedef void (^TextFieldAlertOkBlock)(NSString *input);
typedef void (^TextFieldAlertCancelBlock)(void);

@interface TextFieldAlert : NSObject

+ (void)ask:(NSString*)title
    message:(NSString*)message
         ok:(TextFieldAlertOkBlock)ok
     cancel:(TextFieldAlertCancelBlock)cancel;

@end

TextFieldAlert.m

#import "TextFieldAlert.h"

@implementation TextFieldAlert

+ (void)ask:(NSString*)title
    message:(NSString*)message
         ok:(TextFieldAlertOkBlock)ok
     cancel:(TextFieldAlertCancelBlock)cancel {
    
    // Alert the user.
    UIAlertController *pendingAlert = [UIAlertController
                                       alertControllerWithTitle:title
                                       message:message
                                       preferredStyle:UIAlertControllerStyleAlert];
    
    // Cancel button.
    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action) {
                                       if (cancel != nil) {
                                           cancel();
                                       }
                                   }];
    
    // OK button.
    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:@"OK"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action) {
                                   if (ok != nil) {
                                       UITextField *textField = pendingAlert.textFields[0];
                                       ok(textField.text);
                                   }
                               }];
    
    [pendingAlert addTextFieldWithConfigurationHandler:nil];
    
    // Add buttons to alert.
    [pendingAlert addAction:cancelAction];
    [pendingAlert addAction:okAction];
    
    // Display alert.
    UIViewController* activeController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [activeController presentViewController:pendingAlert animated:YES completion:nil];
}

@end

You can use it like so.

[TextFieldAlert ask:@"Title"
            message:@"Message"
                 ok:^(NSString *input) {
                     NSLog(@"%@", input);
                   }
             cancel:nil];
Posted on 2018-04-06   #ios     #objc  






← Next post    ยท    Previous post →