如何在UIAlertController中添加textField?

在这里输入图像说明

在这里输入图像说明

我想实现一个关于更改密码的function,它需要用户input他以前的密码,并且我devise了一个警报对话框,我想点击“确认修改”button,然后跳转到另一个视图控制器来更改密码。我已经写了一些代码,但是我不知道下一刻该怎么写。

您可以添加多个文本框来提醒控制器,并使用警报控制器的textFields属性访问它们

如果新密码为空string,则重新提示。 或者另一种方法..首先禁用确认button,只有当文本字段有文本时启用它

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

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

Swift 3:

 let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert) alertController.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in textField.placeholder = "Current password" textField.isSecureTextEntry = true }) let confirmAction = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in print("Current password \(String(describing: alertController.textFields?[0].text))") //compare the current password and do action here }) alertController.addAction(confirmAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in print("Canelled") }) alertController.addAction(cancelAction) present(alertController, animated: true, completion: { _ in }) 

注意: alertController.textFields?[0] .text是可选的,在使用前解包

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

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

这是Swift 4.0的一个更新的答案,它创build了所需types的文本字段:

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

以及第一次呈现时的样子: 在这里输入图像说明

而接受文字:

在这里输入图像说明

SWIFT中的[更新答案]

  let PasswordAlert = UIAlertController(title: "Change Password", message: "", preferredStyle: .alert) PasswordAlert.addTextField { (_ textField: UITextField) in textField.placeholder = "Type Password" } let ChangeAction = UIAlertAction(title: "Change", style: .default) { UIAlertAction in print("### This is typed in password -> \(PasswordAlert.textFields?[0].text)") } PasswordAlert.addAction(PlusAction) self.present(PasswordAlert, animated: true, completion: nil)