Assam Al-Zookery Assam Al-Zookery - 4 months ago 33
iOS Question

Display an Alert with Text Field Entry

How to display a text entry field in an alert to get input from the user and use that input back in the application (display the input on a label ) ?

As showing below
enter image description here
enter image description here

Thank you for your time.

Answer
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Text"
                                                                message:@"Enter some text below"
                                                         preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *submit = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {

                                                   if (alert.textFields.count > 0) {

                                                       UITextField *textField = [alert.textFields firstObject];

                                                       textField.text // your text
                                                   }

                                               }];

[alert addAction:submit];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"something"; // if needs
}];

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