IOS开发中UIAlertController(警告框)的使用
2016-05-06 12:06
477 查看
步骤一、初始化:
步骤二、添加按钮:
步骤三、添加一个UITextField用来输入:
步骤四、呈现:
例子:
1、上面添加一个简单的知道了按钮:
2、上面添加取消、确定、以及三个文本输入框(用来作为注册账号的弹出框):
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:^{ ; }]; } } }]];
相关文章推荐
- UIView用户事件响应
- require.js的用法
- 解决 jersey javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
- ios开发之View属性hidden, opaque, alpha, opacity的区别
- 去除 UISearchBar 多余的黑线
- iOS开发系列--UITableView全面解析
- Number Sequence(HDU 1005 构造矩阵 )
- ios中获得UUID的方法,ios怎么获得uuid
- Intellij Idea 12 生成serialVersionUID的方法
- 让UITableView的表头随着tableView一起滚动
- 动态获取UILabel的bounds
- UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)
- UITableView设置单元格选中后只显示一个打勾的三种简单方法(仅供参考)
- ios学习笔记之UIView
- Android UI性能优化实战 识别绘制中的性能问题
- UITextField左侧的表达图片(..我也不知道叫什么)
- DelayQueue的原理和使用浅谈
- Leetcode 303 Range Sum Query - Immutable
- UE4 下载4.11.2的时候:当前遇到连接问题,正在重试
- chmod命令高级篇( SetUID与SetGID标识位)