博客
关于我
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/

你可能感兴趣的文章
【科学计算】插值理论
查看>>
java发送post请求,使用multipart/form-data的方式传递参数,可实现服务器间文件上传功能(转)
查看>>
centos7一步一步搭建docker jenkins 及自定义访问路径重点讲解
查看>>
Java 读取Excel百分数保留原格式(即不转换为小数)的方法
查看>>
git push 时 failed to push some refs 的解决方案
查看>>
深度学习一:深度前馈网络和反向传播
查看>>
Linux多cuda版本切换
查看>>
在wxPython使ListCtrl占据整个窗口
查看>>
微软面试题
查看>>
Google新玩法(转载)
查看>>
C#中Dispose和Close的区别!
查看>>
招聘C#程序员
查看>>
绝密:Google 秘密测试新版首页, 将闪聊嵌入搜索框下方!!
查看>>
如何让服务在流量暴增的情况下保持稳定输出
查看>>
一个20年技术老兵的 2020 年度技术总结
查看>>
EF保存平面数据到SqlServer
查看>>
一例完整的websocket实现群聊demo
查看>>
SQLSERVER数据库死锁与优化杂谈
查看>>
【Net】ABP框架学习之它并不那么好用
查看>>
Git 笔记
查看>>