如何在UIAlertController中添加textField? [英] How to add textField in UIAlertController?

查看:346
本文介绍了如何在UIAlertController中添加textField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现有关更改密码的功能.它要求用户在警报对话框中输入以前的密码.

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

推荐答案

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

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

这篇关于如何在UIAlertController中添加textField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆