Search code examples
iosobjective-cuialertcontroller

How to add text input in alertview of ios 8?


I want to add text input in alert-view of ios 8. I know it was done using UIAlertController but not have any idea. How to do it ?


Solution

  • Screenshot

    Code

     UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Login"
                                                                                      message: @"Input username and password"
                                                                                  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"name";
            textField.textColor = [UIColor blueColor];
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.borderStyle = UITextBorderStyleRoundedRect;
        }];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"password";
            textField.textColor = [UIColor blueColor];
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.secureTextEntry = YES;
        }];
        [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSArray * textfields = alertController.textFields;
            UITextField * namefield = textfields[0];
            UITextField * passwordfiled = textfields[1];
            NSLog(@"%@:%@",namefield.text,passwordfiled.text);
    
        }]];
        [self presentViewController:alertController animated:YES completion:nil];