How to add text input in alertview of ios 8?

40,973

Solution 1

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];

Solution 2

AlertViewController

        // use UIAlertController
        UIAlertController *alert= [UIAlertController
                                      alertControllerWithTitle:@"Title"
                                      message:@"SubTitle"
                                      preferredStyle:UIAlertControllerStyleAlert];

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

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

                                                           NSLog(@"cancel btn");

                                                           [alert dismissViewControllerAnimated:YES completion:nil];

                                                       }];

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

        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"placeHolderText";
            textField.keyboardType = UIKeyboardTypeDefault;
        }];

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

UIAlertView

        UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Title"
                                                         message:@"SubTitle"
                                                        delegate:self
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"OK", nil];

        dialog.alertViewStyle = UIAlertViewStylePlainTextInput;

        [dialog show];

    }

Solution 3

Example of implementation with Swift 3:

var textField: UITextField?

// create alertController
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
  alertController.addTextField { (pTextField) in
  pTextField.placeholder = "usefull placeholdr"
  pTextField.clearButtonMode = .whileEditing
  pTextField.borderStyle = .none
  textField = pTextField
}

// create cancel button
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
  alertController.dismiss(animated: true, completion: nil)
}))

// create Ok button
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (pAction) in
  // when user taps OK, you get your value here
  let inputValue = textField?.text
  alertController.dismiss(animated: true, completion: nil)
}))

// show alert controller
self.present(alertController, animated: true, completion: nil)

Solution 4

UIAlertView *myView = [[UIAlertView alloc]initWithTitle:@"Input" message:@"Enter your value" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
myView.alertViewStyle = UIAlertViewStylePlainTextInput;
[myView textFieldAtIndex:0].delegate = self;
[myView show];

you can cover this way .Thanks

Solution 5

UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

av.alertViewStyle = UIAlertViewStylePlainTextInput;

[av textFieldAtIndex:0].delegate = self;
[av show];

also, you will need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.

and if you user uialertcontroller then use this one

UIAlertController * alert=   [UIAlertController
                              alertControllerWithTitle:@"My Title"
                              message:@"Enter User Credentials"
                              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                           handler:^(UIAlertAction * action) {
                                               //Do Some action here

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

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

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Username";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Password";
    textField.secureTextEntry = YES;
}];

[self presentViewController:alert animated:YES completion:nil];
Share:
40,973
Aarti Oza
Author by

Aarti Oza

Updated on July 09, 2022

Comments

  • Aarti Oza
    Aarti Oza almost 2 years

    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 ?

  • Scotty
    Scotty over 5 years
    You must not refer to alertController.textFields from within the action block. Creates a circular reference. Create __block variables instead.