60

I want to realize a function about changing password. It requires users to input their previous password in an 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.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Change password" message:@"Please input your previous password" preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"please input your previous password";
    textField.secureTextEntry = YES;
}];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handlers:nil];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Confirm the modification" style:UIAlertActionStyleDestructive handler:*(UIAlertAction *alertAction) {
    if (condition) {
        statements
    }
}];

[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

enter image description here

pkamb
  • 33,281
  • 23
  • 160
  • 191
Juice007
  • 1,054
  • 1
  • 9
  • 14

4 Answers4

104

You will get all added textfields from alert controller by its textFields readonly property, you can use it to get its text. Like

Swift 4:

let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
alertController.addTextField { textField in
    textField.placeholder = "Password"
    textField.isSecureTextEntry = true
}
let confirmAction = UIAlertAction(title: "OK", style: .default) { [weak alertController] _ in
    guard let alertController = alertController, let textField = alertController.textFields?.first else { return }
    print("Current password \(String(describing: textField.text))")
    //compare the current password and do action here
}
alertController.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)

Note: textField.text is optional, unwrap it before using

Objective-C:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.placeholder = @"Current password";
    textField.secureTextEntry = YES;
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"Current password %@", [[alertController textFields][0] text]);
    //compare the current password and do action here

}];
[alertController addAction:confirmAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"Canelled");
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

By [[alertController textFields][0] text] this line, it will take first textfield added to the alerController and get its text.

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
Johnykutty
  • 12,091
  • 13
  • 59
  • 100
42

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

If the new password is an empty string, present the alert again. Or another way would be to disable the "Confirm" button, enabling 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

    }
    else{
        [self presentViewController:alertController animated:YES completion:nil];
    }
}];
shim
  • 9,289
  • 12
  • 69
  • 108
Charmi Gheewala
  • 818
  • 7
  • 9
  • the key is how to get the textfield in alertcontroller.So I think this line is very important ! UITextField *password = alertController.textFields.firstObject; When I know this code , I can write the next code. – Juice007 Dec 29 '15 at 05:00
  • How to disable Confirm button – Pankaj Yadav Feb 21 '17 at 07:50
  • 3
    Referencing `alertController` within the action's block leads to a retain cycle. You should only refer to it via weak reference: `__weak typeof(alertController) weakAlert = alertController;` ... `UIAlertAction *...` `...password = weakAlert.textFields.firstObject;` – beebcon May 04 '17 at 22:12
15

Here is an updated answer for Swift 4.0 that creates the desired kind of textfield:

// Create a standard UIAlertController
let alertController = UIAlertController(title: "Password Entry", message: "", preferredStyle: .alert)

// Add a textField to your controller, with a placeholder value & secure entry enabled
alertController.addTextField { textField in
    textField.placeholder = "Enter password"
    textField.isSecureTextEntry = true
    textField.textAlignment = .center
}

// A cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
    print("Canelled")
}

// This action handles your confirmation action
let confirmAction = UIAlertAction(title: "OK", style: .default) { _ in
    print("Current password value: \(alertController.textFields?.first?.text ?? "None")")
}

// Add the actions, the order here does not matter
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)

// Present to user
present(alertController, animated: true, completion: nil)

And how it looks when first presented: enter image description here

And while accepting text:

enter image description here

CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • Hi, I found this very useful however it adds a UITextField to the UIViewController as well as allowing it to be used in the UIAlertController. Is there a way to stop this or to remove it after you click Cancel / OK? – Ryann786 Apr 30 '18 at 23:00
  • Thanks, quick solution – Álvaro Agüero Mar 18 '19 at 22:08
0

Swift 5.1

enter image description here

@objc func promptAddDialog() {
    let ac = UIAlertController.init(title: "Enter answer", message: nil, preferredStyle: .alert)
    ac.addTextField{ textField in
        textField.placeholder = "Answer"
        textField.textAlignment = .center
    }
    
    let submitAction = UIAlertAction(title: "Submit", style: .default) {
    [weak self, weak ac] _ in
    guard let answer = ac?.textFields?[0].text else { return }
    self?.submit(answer)
    }
    ac.addAction(submitAction)
    present(ac, animated: true)
}
logeshpalani31
  • 1,416
  • 12
  • 33