【问题标题】:How to add textField in UIAlertController?如何在 UIAlertController 中添加 textField?
【发布时间】:2016-04-02 22:33:45
【问题描述】:

我想实现一个修改密码的功能。它要求用户在警告对话框中输入他们以前的密码。

我想点击“确认修改”按钮然后跳转到另一个视图控制器来更改密码。我已经写了一些代码,但我不知道下一刻该怎么写。

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

【问题讨论】:

标签: ios objective-c uitextfield uialertcontroller


【解决方案1】:

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

斯威夫特 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并获取它的文本。

【讨论】:

  • 我想知道这段代码“[alertController textFields] [0] text”是什么意思?
  • @Juice007 这意味着您正在获取警报控制器的第一个文本字段并获取其文本,非常简单
  • 您应该使用[weak alertController] 来避免保留周期。
  • 感谢您提供出色而简单的解决方案。
【解决方案2】:

您可以向警报控制器添加多个文本字段并使用警报控制器的textFields 属性访问它们

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

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

【讨论】:

  • 关键是如何获取alertcontroller中的textfield。所以我觉得这一行很重要! UITextField *password = alertController.textFields.firstObject;知道了这段代码,就可以写下一段代码了。
  • 如何禁用确认按钮
  • 在操作块中引用 alertController 会导致保留周期。你应该只通过弱引用来引用它:__weak typeof(alertController) weakAlert = alertController; ... UIAlertAction *... ...password = weakAlert.textFields.firstObject;
【解决方案3】:

这是 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)

以及首次展示时的外观:

在接受文本时:

【讨论】:

  • 嗨,我发现这非常有用,但是它向 UIViewController 添加了一个 UITextField 并允许在 UIAlertController 中使用它。点击取消/确定后有没有办法停止或删除它?
  • 谢谢,快速解决
【解决方案4】:

斯威夫特 5.1

@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)
}

【讨论】:

    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多