博客
关于我
iOS UIAlertController
阅读量:423 次
发布时间:2019-03-06

本文共 4812 字,大约阅读时间需要 16 分钟。

在Xcode的iOS9.0 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代。

在iOS 9中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。

1、对话框

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"message:@"这个是UIAlertController的默认样式"preferredStyle:UIAlertControllerStyleAlert];        [self presentViewController:alertController animated:YES completion:nil];

同创建UIAlertView相比,我们无需指定代理,也无需在初始化过程中指定按钮。不过要特别注意第三个参数,要确定您选择的是对话框样式还是上拉菜单样式。

 

2、将动作按钮添加到控制器

通过创建UIAlertAction的实例,您可以将动作按钮添加到控制器上。UIAlertAction由标题字符串、样式以及当用户选中该动作时运行的代码块组成。通过UIAlertActionStyle,您可以选择如下三种动作样式:常规(default)、取消(cancel)以及警示(destruective)。为了实现原来我们在创建UIAlertView时创建的按钮效果,我们只需创建这两个动作按钮并将它们添加到控制器上即可。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"message:@"这个是UIAlertController的默认样式"preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];    [alertController addAction:cancelAction];    [alertController addAction:okAction];    [self presentViewController:alertController animated:YES completion:nil];

3、“警示”样式

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"message:@"这个是UIAlertController的默认样式"preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];        //添加顺序和显示顺序相同    [alertController addAction:cancelAction];    [alertController addAction:resetAction];    [self presentViewController:alertController animated:YES completion:nil];

 

4、文本对话框

设置输入框,并且设置通知,检测输入字符长度少于3时,不能点击“好的”。

值得注意的是,不像使用UIAlertView的时候可以用UIAlertViewDelegate检测文本框输入,UIAlertController没有对应的代理,只能自己实现监听。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];        textField.placeholder = @"登录";    }];        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];        textField.placeholder = @"密码";        textField.secureTextEntry = YES;    }];            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        UITextField *login = alertController.textFields.firstObject;        UITextField *password = alertController.textFields.lastObject;        NSLog(@"%@",login.text);        NSLog(@"%@",password.text);    }];        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];    [alertController addAction:cancelAction];    [alertController addAction:okAction];            [self presentViewController:alertController animated:YES completion:nil];

 

- (void)alertTextFieldDidChange:(NSNotification *)notification{    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;    if (alertController) {                UITextField *login = alertController.textFields.firstObject;        UIAlertAction *okAction = alertController.actions.lastObject;        okAction.enabled = login.text.length > 2;    }}

 

5、上拉菜单

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存或删除数据" message:@"删除数据将不可恢复" preferredStyle: UIAlertControllerStyleActionSheet];        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {        NSLog(@"点击了取消");    }];        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {        NSLog(@"点击了删除");    }];    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {        NSLog(@"点击了保存");    }];            [alertController addAction:cancelAction];    [alertController addAction:deleteAction];    [alertController addAction:archiveAction];        [self presentViewController:alertController animated:YES completion:nil];

 

 

转载地址:http://mhfyz.baihongyu.com/

你可能感兴趣的文章
IO多路复用小故事
查看>>
纠错码简介
查看>>
码云 Pages 搭建
查看>>
《论可计算数及其在判定上的应用》简单理解
查看>>
中国剩余定理证明过程
查看>>
java中Object.equals()简单用法
查看>>
poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
查看>>
poj 2492A Bug's Life(并查集)
查看>>
POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
查看>>
java中自动装箱的问题
查看>>
zyUpload+struct2完成文件上传
查看>>
程序员的开发文档
查看>>
mybatis generator修改默认生成的sql模板
查看>>
cglib动态代理导致注解丢失问题及如何修改注解允许被继承
查看>>
算法 - 如何从股票买卖中,获得最大收益
查看>>
大数据开发-Spark-拷问灵魂的5个问题
查看>>
算法 - 链表操作思想 && case
查看>>
并发编程实战-ConcurrentHashMap源码解析
查看>>
C#之反射、元数据详解
查看>>
通俗易懂设计模式解析——单例模式
查看>>