我想实现一个关于修改密码的功能.它要求用户在alert 对话框中输入以前的密码.

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

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

enter image description here

推荐答案

您可以向alert controller添加多个文本字段,并使用alert controller的textFields属性访问它们

如果新密码为空字符串,请再次显示alert .或者另一种方法是禁用"确认"按钮,仅当文本字段中有文本时才启用它.

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

Objective-c相关问答推荐

iOS 7 表格视图无法自动调整内容插入

iOS:警告try 呈现其视图不在窗口层次 struct 中的 ViewController

通过指针算术访问数组值与 C 中的下标

ios10:viewDidLoad 框架宽度/高度未正确初始化

如何向 UILabel 添加滚动功能

使用 NSArray 初始化对象数组

单元测试私有方法 - 目标 C

自动属性合成 (@property) 和继承

如何以编程方式暂停 NSTimer?

启动 Finder 窗口并 Select 特定文件

将子视图添加到 UIButton

Objective-C 中的 const 与静态 NSStrings

WKWebView 判断 JavaScript 返回值

如何对包含数字的 NSMutable 数组进行排序?

如何在 Objective-C 中将 NSString 解析为 BOOL?

如何将 nil 添加到 nsmutablearray?

如何从设置屏幕打开位置服务屏幕?

强制iOS应用程序崩溃的最快方法是什么?

无法在导航栏中心设置titleView,因为返回按钮

AppDelegate 的当前视图控制器?