首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在UIAlertController中添加textField?

如何在UIAlertController中添加textField?
EN

Stack Overflow用户
提问于 2015-12-29 12:13:44
回答 3查看 41.4K关注 0票数 55

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

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

代码语言:javascript
运行
复制
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];

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-29 12:19:25

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

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

代码语言:javascript
运行
复制
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];
    }
}];
票数 40
EN

Stack Overflow用户

发布于 2017-12-28 08:54:12

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

代码语言:javascript
运行
复制
// 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)

以及它第一次呈现时的外观:

在接受文本的同时:

票数 15
EN

Stack Overflow用户

发布于 2021-08-18 18:29:20

Swift 5.1

代码语言:javascript
运行
复制
@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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34504317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档