UIKit框架-07.UIAlertController使用
2015-07-13 22:06
483 查看
1.UIAlertController概述
UIAlertController是IOS 8.0之后出现的新特性,它同时结合了UIAlertView和UIActionSheet的功能IOS 8.0之前我们还是需要使用UIAlertView和UIActionSheet,完成弹框功能
2.使用UIAlertController实现UIAlertView效果
实现UIAlerView-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ // 1.创建UIAlertControllerStyleAlert样式的UIAlertController UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert]; // 2.创建按钮 /* UIAlertActionStyleDefault = 0, //默认样式 UIAlertActionStyleCancel, // 字体加粗 UIAlertActionStyleDestructive // 强调,字体标红 */ UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action1被点击了"); }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action2被点击了"); }]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action3被点击了"); }]; // 2.1 添加按钮 [alertVc addAction:action1]; [alertVc addAction:action2]; [alertVc addAction:action3]; // 3.添加文本框 [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框1"); }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框2"); textField.secureTextEntry = YES;//设置密文 }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框3"); }]; //4.添加显示控制器方法 [self presentViewController:alertVc animated:YES completion:nil]; }
效果图
注意:我们使用UIAlertView最多只能创建2个文本输入框,而实用UIAlertController可以创建多个文本输入框
3.实现UIActionSheet效果
实现UIActionSheet-(void)setAlertview{ // 1.创建UIAlertControllerStyleAlert样式的UIAlertController UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert]; // 2.创建按钮 /* UIAlertActionStyleDefault = 0, //默认样式 UIAlertActionStyleCancel, // 字体加粗 UIAlertActionStyleDestructive // 强调,字体标红 */ UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action1被点击了"); }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action2被点击了"); }]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件 NSLog(@"action3被点击了"); }]; // 2.1 添加按钮 [alertVc addAction:action1]; [alertVc addAction:action2]; [alertVc addAction:action3]; // 3.添加文本框 [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框1"); }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框2"); textField.secureTextEntry = YES;//设置密文 }]; [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) { NSLog(@"文本框3"); }]; //4.添加显示控制器方法 [self presentViewController:alertVc animated:YES completion:nil]; }
效果图
注意:
如果UIAlertController的样式是ActionSheet, 就不能添加输入框
reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'
UIAlertController继承UIController类,而UIAlertView和UIActionSheet继承UIView,所以要想在视图上显示UIAlertController,需要调用如下方法
[self presentViewController:alertVc animated:YES completion:nil];
相关文章推荐
- [UI] 精美UI界面欣赏[12]
- UI--Android中的状态切换按钮自定义
- UIKit框架 - 06.UIActionSheet使用
- vs2013错误,错误 1 error MSB8020: The build tools for Visual Studio 2012
- Win7安vc2008编译报LINK : fatal error LNK1000: Internal error during IncrBuildImage
- Android(java)学习笔记127:Android Studio新建工程中的build.gradle、settings.gradle
- IOS UITableView 概述
- UIKit框架-05.UIAlertView使用
- Android(java)学习笔记126:Android Studio中build.gradle简介
- 代码手写UI、xib和storyboard各自的局限性
- UICollectionView-使用Flow Layout
- IOS UI设计模式 - 分页
- amaze ui 的使用
- 浅谈C#多线程与UI响应
- AndroidAnnotations框架@Ebean,@RootContext,@Background,@UiThread,@AfterInject,@AfterTextChange标签的使用方法
- easyui中datagrid和layout的问题
- hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理
- NGUI ScrollView总结
- EasyUI-DataGrid多线动态实现选择性合并
- IOS中 什么是UITableView的索引放大镜字符