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. }

效果如图:

IOS8 : UIAlertController的更多相关文章

  1. IOS8 UIAlertController 弹框

    本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35551769 IOS8中,Apple将UIActionSheet和UIAlertVi ...

  2. iOS8 UIAlertController弹出框中添加视图(例如日期选择器等等)

    UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerMode ...

  3. 升级IOS8游戏上传自定义头像功能失效的问题

    为了支持arm64,之前已经折腾了很久,昨晚打包准备提交苹果审核时,测试那边的同事反馈说游戏上传自定义头像功能不可用了. 游戏上传自定义功能的简介:卡牌游戏最初是<比武招亲>中有一个充VI ...

  4. UIAlertController custom font, size, color

    本文转载至 http://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color up vote2d ...

  5. iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色

    废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...

  6. iOS开发之UIAlertController的适配

    在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertControlle ...

  7. UIActionSheet 修改字体颜色

    -(void)willPresentActionSheet:(UIActionSheet *)actionSheet { SEL selector = NSSelectorFromString(@&q ...

  8. UIAlertViewController+TextField 输入框

    if (IOS8) { UIAlertController *alertController=[UIAlertController alertControllerWithTitle:CustomLoc ...

  9. runtime查找 UIAlertAction 的key 及 UIActionSheet 设置字体颜色

    修改不了颜色了 结果发现kvo 的key 不对 哎 直接上代码 设置正确的属性找到对应的key  还以为iOS 11改变了方法 unsigned int count; Ivar *ivars =  c ...

随机推荐

  1. STM32 USB 问题汇总(转)

    源:STM32 USB 问题汇总 汇总1:STM32的USB例程修改步骤,来自http://blog.csdn.net/cy757/archive/2010/01/01/5117610.aspx 以下 ...

  2. Word中的公式向上偏或向下偏的解决方法

    在word 2010中,发现公式无法与文字排成一行时,可选中文字,然后点“字体”,然后“高级”选项中选择“位置”,然后根据不同情况选择“标准”.“提升”.“降低”.

  3. *CCF 201612-2 工资计算(80)

    试题编号:201612-2 试题名称:工资计算 时间限制:1.0s 内存限制:256.0MB 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假设他一个月的税前工资 ...

  4. 最新的thinkphp 后台入口的问题

    新的thinkphp后台入口绑定了单独了文件 admin.php 要注意.

  5. 10-instanceof

    在Java中可以使用instanceof关键字判断一个对象到底是不是一个类的实例 package com.example; class A{ void tell1(){ System.out.prin ...

  6. Sublime Text 3 安装 Emmet 插件

    首先安装 Package Control 1.通过快捷键 ctrl+` 或者 View > Show Console 菜单打开控制台 2.粘贴以下代码后回车安装 import  urllib.r ...

  7. python py_innodb_page_info.py -v /usr/local/var/mysql/ibdata1

    mylib.py #encoding=utf-8 import os import include from include import * TABLESPACE_NAME='D:\\mysql_d ...

  8. window.open a.href打开窗口referer的问题

    window.open a.href打开窗口referer的问题: JSP: <%@ page language="java" import="java.util. ...

  9. JavaWeb知识回顾二

    动态web资源相关 1.tomcat相关 tomcat的目录结构 bin -- tomcat服务器的批处理文件的存放目录 conf -- tomcat服务器配置文件的存放目录 lib -- tomca ...

  10. 如何在NodeJS项目中优雅的使用ES6

    如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...