您的位置:首页 > 产品设计 > UI/UE

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

2016-05-06 12:06 477 查看
步骤一、初始化:

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:0];
UITextField *mima1 = [registerAlertC.textFields objectAtIndex:1];
UITextField *mima2 = [registerAlertC.textFields objectAtIndex:2];

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:^{
;
}];

}

}

}]];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: