步骤一、初始化:
1 UIAlertController * inputname = [UIAlertController alertControllerWithTitle:@"未输入账户" message:@"请输入账户名" preferredStyle:UIAlertControllerStyleAlert];
步骤二、添加按钮:
1 [inputname addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 2 3 ;//点击知道了之后可以出发的事件 4 5 }]];
步骤三、添加一个UITextField用来输入:
1 [inputname addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 2 textField.placeholder = @"请再次输入密码"; 3 textField.clearButtonMode = UITextFieldViewModeWhileEditing; 4 }];
步骤四、呈现:
1 [self presentViewController:inputname animated:YES completion:^{ 2 ;//呈现出来之后执行的事件 3 }];
例子:
1、上面添加一个简单的知道了按钮:
1 // 1、创建一个UIAlertController并初始化 2 UIAlertController * inputname = [UIAlertController alertControllerWithTitle:@"未输入账户" message:@"请输入账户名" preferredStyle:UIAlertControllerStyleAlert]; 3 // 2、添加一个“知道了”按钮 4 [inputname addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 5 6 ;//点击知道了之后可以出发的事件 7 8 }]]; 9 // 3、把设置好的UIAlertController呈现在屏幕上 10 [self presentViewController:inputname animated:YES completion:^{ 11 ;//呈现出来之后执行的事件 12 }];
2、上面添加取消、确定、以及三个文本输入框(用来作为注册账号的弹出框):
1 UIAlertController * registerAlertC = [UIAlertController alertControllerWithTitle:@"注册新用户" message:nil preferredStyle:UIAlertControllerStyleAlert]; 2 3 4 [registerAlertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 5 textField.placeholder = @"请输入用户名"; 6 textField.clearButtonMode = UITextFieldViewModeWhileEditing; 7 }]; 8 9 [registerAlertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 10 textField.placeholder = @"请输入密码"; 11 textField.clearButtonMode = UITextFieldViewModeWhileEditing; 12 }]; 13 14 [registerAlertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 15 textField.placeholder = @"请再次输入密码"; 16 textField.clearButtonMode = UITextFieldViewModeWhileEditing; 17 }]; 18 19 [registerAlertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 20 ; 21 }]]; 22 23 [registerAlertC addAction:[UIAlertAction actionWithTitle:@"注册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 24 UITextField *name = [registerAlertC.textFields objectAtIndex:0]; 25 UITextField *mima1 = [registerAlertC.textFields objectAtIndex:1]; 26 UITextField *mima2 = [registerAlertC.textFields objectAtIndex:2]; 27 28 if ([name.text isEqualToString:@""]) { 29 UIAlertController * worning1 = [UIAlertController alertControllerWithTitle:@"用户名不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert]; 30 [worning1 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 31 ; 32 }]]; 33 [self presentViewController:worning1 animated:YES completion:^{ 34 ; 35 }]; 36 37 }else{ 38 if ([mima2.text isEqualToString:mima1.text]) { 39 // 注册成功 40 // 使用单例把账户密码保存起来 41 NSString *mima = mima1.text; 42 NSString *zhanghu = name.text; 43 44 NSUserDefaults *User =[NSUserDefaults standardUserDefaults]; 45 [User setObject:mima forKey:zhanghu]; 46 UIAlertController * success = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert]; 47 [success addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 48 ; 49 }]]; 50 [self presentViewController:success animated:YES completion:^{ 51 ; 52 }]; 53 54 55 56 57 58 }else{ 59 // 两次输入密码不一致 60 UIAlertController * worning2 = [UIAlertController alertControllerWithTitle:@"两次输入密码不一致" message:nil preferredStyle:UIAlertControllerStyleAlert]; 61 [worning2 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 62 ; 63 }]]; 64 [self presentViewController:worning2 animated:YES completion:^{ 65 ; 66 }]; 67 68 } 69 70 71 72 } 73 74 75 }]];