UIAlertController 和  UIAlertAction 用法:

1. 最简单的提醒视图:

这里我们实现一个最简单的提醒视图,包含1个标题,1行信息,1个按键,按下按键后,什么都不发生:

  1. - (IBAction)doAlert:(id)sender {
  2. // 准备初始化配置参数
  3. NSString *title = @"Alert Button Selected";
  4. NSString *message = @"I need your attention NOW!";
  5. NSString *okButtonTitle = @"OK";
  6. // 初始化
  7. UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  8. // 创建操作
  9. UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  10. // 操作具体内容
  11. // Nothing to do.
  12. }];
  13. // 添加操作
  14. [alertDialog addAction:okAction];
  15. // 呈现警告视图
  16. [self presentViewController:alertDialog animated:YES completion:nil];
  17. }

进入程序后,点击“Alert Me!”按钮可触发这个提醒框,如图所示:

       
2. 多个按键的提醒视图
       这里我们实现一个最简单的提醒视图,包含1个标题,1行信息,3个按键,按下按键后,标签显示按下的按键名称:
  1. - (IBAction)doMultiButtonAlert:(id)sender {
  2. // 准备初始化配置参数
  3. NSString *title = @"Alert Button Selected";
  4. NSString *message = @"I need your attention NOW!";
  5. NSString *okButtonTitle = @"OK";
  6. NSString *neverButtonTitle = @"Never";
  7. NSString *laterButtonTitle = @"Maybe Later";
  8. // 初始化
  9. UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  10. // 分别3个创建操作
  11. UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  12. // 普通按键
  13. self.userOutput.text = @"Clicked 'Maybe Later'";
  14. }];
  15. UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  16. // 红色按键
  17. self.userOutput.text = @"Clicked 'Never'";
  18. }];
  19. UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  20. // 取消按键
  21. self.userOutput.text = @"Clicked 'OK'";
  22. }];
  23. // 添加操作(顺序就是呈现的上下顺序)
  24. [alertDialog addAction:laterAction];
  25. [alertDialog addAction:neverAction];
  26. [alertDialog addAction:okAction];
  27. // 呈现警告视图
  28. [self presentViewController:alertDialog animated:YES completion:nil];
  29. }

3个按键分别代表了3种不同类型的按键,分别是默认按键(普通)、销毁按键(红色)和取消按键(粗体)。从代码看其实就是在上一个的基础上加了3个 UIAlertAction 而已,然后分别设置不同的 style,效果如下:

3. 带输入框的提醒视图
       如何添加输入框呢?新的 iOS 8 提供了相应的接口,使增加输入框就像增加按键方法一样简单。这里还是在第1个方法的基础上改动。
  1. - (IBAction)doAlertInput:(id)sender {
  2. // 准备初始化配置参数
  3. NSString *title = @"Email Address";
  4. NSString *message = @"Please enter your your email address:";
  5. NSString *okButtonTitle = @"OK";
  6. // 初始化
  7. UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  8. // 创建文本框
  9. [alertDialog addTextFieldWithConfigurationHandler:^(UITextField *textField){
  10. textField.placeholder = @"Your Email";
  11. textField.secureTextEntry = NO;
  12. }];
  13. // 创建操作
  14. UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  15. // 读取文本框的值显示出来
  16. UITextField *userEmail = alertDialog.textFields.firstObject;
  17. self.userOutput.text = userEmail.text;
  18. }];
  19. // 添加操作(顺序就是呈现的上下顺序)
  20. [alertDialog addAction:okAction];
  21. // 呈现警告视图
  22. [self presentViewController:alertDialog animated:YES completion:nil];
  23. }

在创建操作前先创建文本框,以便后面的按键可以操作文本框内容。创建文本框也只是用了一个简单的方法而已,想创建更多文本框就再使用多次这个方法即可,程序效果如下:


4. 提醒图表
       与第2个和第3个方法相比,创建提醒图表简直易如反掌。因为和第1个方法相比,只需要改动一个参数就可以,即把创建UIAlertController实例的参数 UIAlertControllerStyleAlert 改为 UIAlertControllerStyleActionSheet ,别的都不用变。
  1. - (IBAction)doActionSheet:(id)sender {
  2. // 准备初始化配置参数
  3. NSString *title = @"Alert Button Selected";
  4. NSString *message = @"I need your attention NOW!";
  5. NSString *okButtonTitle = @"OK";
  6. NSString *neverButtonTitle = @"Never";
  7. NSString *laterButtonTitle = @"Maybe Later";
  8. // 初始化
  9. UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
  10. // 分别3个创建操作
  11. UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  12. // 普通按键
  13. self.userOutput.text = @"Clicked 'Maybe Later'";
  14. }];
  15. UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  16. // 红色按键
  17. self.userOutput.text = @"Clicked 'Never'";
  18. }];
  19. UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  20. // 取消按键
  21. self.userOutput.text = @"Clicked 'OK'";
  22. }];
  23. // 添加操作(顺序就是呈现的上下顺序)
  24. [alertDialog addAction:laterAction];
  25. [alertDialog addAction:neverAction];
  26. [alertDialog addAction:okAction];
  27. // 呈现警告视图
  28. [self presentViewController:alertDialog animated:YES completion:nil];
  29. }

效果如图:

最新文章

  1. oracle中一些用法总结
  2. linux shell 字符串操作(长度,查找,替换)详解
  3. POJ 2027
  4. UVaLive 6693 Flow Game (计算几何,线段相交)
  5. GridControl控件添加按钮列及在按钮Click事件中得到行数据 zt
  6. Linux C 编译错误总结
  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01
  8. android自定义View---生成虚线的View
  9. 二叉树的建立以及遍历的多种实现(python版)
  10. MySQL创建数据库指定字符集
  11. 1.4eigen中的块运算
  12. 【读书笔记】iOS-动态类型和动态绑定
  13. Android实践--apk反编译
  14. Jmeter脚本录制方法(二)手工编写脚本(jmeter与fiddler结合使用)
  15. guider – 全系统Linux性能分析器
  16. RelativeLayout.LayoutParams
  17. struts2读取request,session,application中的值
  18. Cocos2d-x游戏移植到WP8之路 -- c++和c#交互
  19. elasticsearch使用笔记
  20. composer 上提交自己的包

热门文章

  1. AutoMapper使用说明
  2. TCP/IP详解--拥塞控制 & 慢启动 快恢复 拥塞避免
  3. 递归添加 另一个ds 里的DataRow 时 报错:该行已经属于另一个表。
  4. iOS bug 之 H5 页面没有弹出提示框
  5. IM 融云 之 列表中显示聊天用户名称
  6. UVa 10057 - A mid-summer night's dream
  7. 2.7. 属性的各种设置选项(Core Data 应用程序实践指南)
  8. js模块化开发——require.js的实战写法1
  9. NodeMCU之旅(一):构建、刷入固件,上传代码
  10. redis 实例