Juice007 Juice007 -4 years ago 165
iOS Question

How to add textField in UIAlertController?

enter image description here

enter image description here

I want to realize a function about changing password,it requires user to input his previous password,and I design it in a alert dialog,I want to click the button "Confirm the modification" then jump to the other view controller for changing password.I have written some code,but I don't know how to write in the next moment.

Answer Source

You can add multiple textfields to alert controller and access them with alert controller's textFields property

If new password is empty string, present the alert again. Or another way.. first disable Confirm button, enable it only when text field has text

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"confirm the modification" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    UITextField *password = alertController.textFields.firstObject;
    if (![password.text isEqualToString:@""]) {

        //change password

        [self presentViewController:alertController animated:YES completion:nil];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download