Return to WrightsCS.com   |   Check out WrightsCSApps.com   |   How to Post Snippets  

Not registered? Thats okay! You can Submit a Snippet without being a member of this site!


     0votes

Here is a fun way to create a generic UIAlertViewController. I have this setup as a class method in my Singleton. You can configure this to have text fields and buttons with the following code:

#define OK          NSLocalizedString(@"OK", nil)
#define CANCEL      NSLocalizedString(@"Cancel", nil)
[Singleton alert:NSLocalizedString(@"Login Form", nil)
         message:NSLocalizedString(@"Enter username and password.", nil)
          fields:@[
                   @{ @"text":@"", @"placeholder":NSLocalizedString(@"Username", nil), @"secure":@NO },
                   @{ @"text":@"", @"placeholder":NSLocalizedString(@"Password", nil), @"secure":@YES }
                   ]
         buttons:@[ CANCEL, OK ]
      controller:self
        animated:YES
      completion:^(BOOL ok, NSArray * values) {
          if ( ok ) {
              NSLog(@"Username: %@", values[0]);
              NSLog(@"Plaintext Password: %@", values[1]);
          }
      }];

In the above example, when you press OK, you will see in the console the username and password printed out. values[0] represents the first textbook in our fields array, and so on. For each dictionary in fields, you can add other values to set up your textfields as well, they just need to be configured in the class method below.

/*!
 *  @param fields @[ @{ @"text":@"string", @"placeholder":@"string", @"secure":@BOOL } ]
 *  @param buttons @[ CANCEL, OK ]
 *  @param controller SELF for UIViewController, NIL for rootViewController
 *  @param completion VALUES stores textfield values in order
 */
+ (void)alert:(NSString*)title message:(NSString*)message fields:(NSArray*)fields buttons:(NSArray*)buttons controller:(UIViewController*)controller animated:(BOOL)animated completion:(void (^)(BOOL ok, NSArray * values))completion
{
    UIAlertController * _alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableArray * _textFieldValues = [NSMutableArray new];
    
    if ( fields != nil )
    {
        for ( NSDictionary * field in fields )
        {
            [_alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                textField.text = field[@"text"];
                textField.placeholder = field[@"placeholder"];
                textField.secureTextEntry = [field[@"secure"] boolValue];
            }];
        }
    }
    
    if ( buttons == nil )
    {
        [_alert addAction:[UIAlertAction actionWithTitle:OK style:UIAlertActionStyleCancel
                                                 handler:^(UIAlertAction * action) {
                                                     
                                                     for ( UITextField * _textField in _alert.textFields ) {
                                                         if ( _textField.text != nil ||
                                                             [_textField.text isEqualToString:@""] ||
                                                             [_textField.text isEqualToString:@" "])
                                                             [_textFieldValues addObject:_textField.text];
                                                     }
                                                     completion(YES, _textFieldValues);
                                                     
                                                     [_alert dismissViewControllerAnimated:animated completion:nil];
                                                 }]];
    }
    else
    {
        for ( NSString * button in buttons )
        {
            if ( [button isEqualToString:OK] )
            {
                [_alert addAction:[UIAlertAction actionWithTitle:OK style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             
                                                             for ( UITextField * _textField in _alert.textFields ) {
                                                                 if ( _textField.text != nil ||
                                                                     [_textField.text isEqualToString:@""] ||
                                                                     [_textField.text isEqualToString:@" "])
                                                                     [_textFieldValues addObject:_textField.text];
                                                             }
                                                             completion(YES, _textFieldValues);
                                                             
                                                             [_alert dismissViewControllerAnimated:animated completion:nil];
                                                         }]];
            }
            if ( [button isEqualToString:CANCEL] )
            {
                [_alert addAction:[UIAlertAction actionWithTitle:CANCEL style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                             
                                                             for ( UITextField * _textField in _alert.textFields ) {
                                                                 if ( _textField.text != nil ||
                                                                     [_textField.text isEqualToString:@""] ||
                                                                     [_textField.text isEqualToString:@" "])
                                                                     [_textFieldValues addObject:_textField.text];
                                                             }
                                                             completion(NO, _textFieldValues);
                                                             
                                                             [_alert dismissViewControllerAnimated:animated completion:nil];
                                                         }]];
            }
        }
    }
    [( controller != nil ? controller : [AppDelegate rootController] ) presentViewController:_alert animated:animated completion:nil];
}

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

© Copyright 2011 Wright's Computer Services.   All rights reserved Suffusion theme by Sayontan Sinha