如何在UIAlertController中添加textField?

时间:2015-12-29 04:13:44

标签: ios objective-c uitextfield uialertcontroller

enter image description here

enter image description here

我想实现一个关于更改密码的功能,它要求用户输入他以前的密码,我在一个警告对话框中设计它,我想点击按钮"确认修改"然后跳转到另一个视图控制器来更改密码。我写了一些代码,但我不知道下一刻该怎么写。

3 个答案:

答案 0 :(得分:71)

您将通过其textFields只读属性从警报控制器获取所有添加的文本字段,您可以使用它来获取其文本。 像

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)

注意: textField.text是可选的,在使用之前将其解包

<强>目标-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];

通过[[alertController textFields][0] text]这一行,它将第一个文本字段添加到alerController并获取其文本。

答案 1 :(得分:34)

您可以向警报控制器添加多个文本字段,并使用警报控制器textFields属性

访问它们

如果新密码为空字符串,请再次显示警报。或者另一种方法是禁用&#34;确认&#34;按钮,仅在文本字段包含文本时启用它。

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

答案 2 :(得分:11)

以下是Swift 4.0的更新答案,它创建了所需类型的文本字段:

// 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)

首次展示时的外观如何: enter image description here

接受文字时:

enter image description here