IOS开发中UIAlertController(警告框)的使用

时间:2022-12-05 21:50:06

步骤一、初始化:

 UIAlertController * inputname = [UIAlertController alertControllerWithTitle:@"未输入账户" message:@"请输入账户名" preferredStyle:UIAlertControllerStyleAlert];

步骤二、添加按钮:

 [inputname addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

             ;//点击知道了之后可以出发的事件

         }]];

步骤三、添加一个UITextField用来输入:

 [inputname addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请再次输入密码";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}];

步骤四、呈现:

 [self presentViewController:inputname animated:YES completion:^{
;//呈现出来之后执行的事件
}];

例子:

1、上面添加一个简单的知道了按钮:

 //       1、创建一个UIAlertController并初始化
UIAlertController * inputname = [UIAlertController alertControllerWithTitle:@"未输入账户" message:@"请输入账户名" preferredStyle:UIAlertControllerStyleAlert];
// 2、添加一个“知道了”按钮
[inputname addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { ;//点击知道了之后可以出发的事件 }]];
// 3、把设置好的UIAlertController呈现在屏幕上
[self presentViewController:inputname animated:YES completion:^{
;//呈现出来之后执行的事件
}];

2、上面添加取消、确定、以及三个文本输入框(用来作为注册账号的弹出框):

 UIAlertController * registerAlertC = [UIAlertController alertControllerWithTitle:@"注册新用户" message:nil preferredStyle:UIAlertControllerStyleAlert];

     [registerAlertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入用户名";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}]; [registerAlertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入密码";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}]; [registerAlertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请再次输入密码";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}]; [registerAlertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
;
}]]; [registerAlertC addAction:[UIAlertAction actionWithTitle:@"注册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *name = [registerAlertC.textFields objectAtIndex:];
UITextField *mima1 = [registerAlertC.textFields objectAtIndex:];
UITextField *mima2 = [registerAlertC.textFields objectAtIndex:]; if ([name.text isEqualToString:@""]) {
UIAlertController * worning1 = [UIAlertController alertControllerWithTitle:@"用户名不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
[worning1 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
;
}]];
[self presentViewController:worning1 animated:YES completion:^{
;
}]; }else{
if ([mima2.text isEqualToString:mima1.text]) {
// 注册成功
// 使用单例把账户密码保存起来
NSString *mima = mima1.text;
NSString *zhanghu = name.text; NSUserDefaults *User =[NSUserDefaults standardUserDefaults];
[User setObject:mima forKey:zhanghu];
UIAlertController * success = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
[success addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
;
}]];
[self presentViewController:success animated:YES completion:^{
;
}]; }else{
// 两次输入密码不一致
UIAlertController * worning2 = [UIAlertController alertControllerWithTitle:@"两次输入密码不一致" message:nil preferredStyle:UIAlertControllerStyleAlert];
[worning2 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
;
}]];
[self presentViewController:worning2 animated:YES completion:^{
;
}]; } } }]];

相关文章