iOS 8 后 UIAlertView 和  UIActionSheet 都被合并到了 UIAlertController里面。

文档原文:

Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.

In apps that run in versions of iOS
prior to iOS 8, use the UIAlertView class to display an alert message to
the user. An alert view functions similar to but differs in appearance
from an action sheet (an instance of UIActionSheet).

Use the
properties and methods defined in this class to set the title, message,
and delegate of an alert view and configure the buttons. You must set a
delegate if you add custom buttons. The delegate should conform to the
UIAlertViewDelegate protocol. Use the show method to display an alert
view once it is configured.

简单介绍一下。

一、 创建一个alert  UIAlertController:

1 UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
2 message:@"This is an alert."
3 preferredStyle:UIAlertControllerStyleAlert];
4
5 UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
6 handler:^(UIAlertAction * action) {}];
7
8 [alert addAction:defaultAction];
9 [self presentViewController:alert animated:YES completion:nil];

UIAlertController中,没有按钮的添加,有的事Title和Message这样的信息,按钮放在我下面要说的UIAlertAction里面;

在preferredStyle中 ,UIAlertController 提供了2中style

UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
分别对应UIActionSheet 和 UIAlertView。

二、 设置好style后需要添加 action,来添加按钮

按钮的事件不再使用代理方法,而是通过Block的方式添加多个按钮。

UIAlertController 也可以添加UITextField, UITextField的设置放在Block内部,可以通过数组alertController.textFields来获取的添加在输入框中的值,同样的也可以添加多个,但是必须在alertView的样式里添加,否则会报错,所以添加UITextField前需判断style是否是UIAlertView。

三、由于UIAlertController 的父类是UIViewController, 所以显示的时候,不是用show方法,而是用推出视图的方式推出。

// 主要代码

#pragma mark 分段控制器事件

- (IBAction)segmentedControlAction:(UISegmentedControl *)sender

{

// 1. 创建UIAlertControl变量,但并不穿GIAn

UIAlertController *alertController = nil;

// 2. 根据点击的item创建不同样式的alertController

switch (sender.selectedSegmentIndex) {

case 0: { // 弹出AlertView

alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];

break;

}

case 1: { // 弹出ActionSheet

alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];

break;

}

default:

break;

}

// 3. 添加取消按钮

// 3.1 UIAlertAction 表示一个按钮,同时,这个按钮带有处理事件的block

UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

NSLog(@"取消");

}];

// 3.2 添加到alertController上

[alertController addAction:action];

// 4. 添加需要谨慎操作的按钮,文字默认是红色的

[alertController addAction:({

UIAlertAction *action = [UIAlertAction actionWithTitle:@"谨慎操作的按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

NSLog(@"谨慎操作的按钮");

}];

action;

})];

// 5. 添加确定按钮

[alertController addAction:({

UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"确定");

// 打印输入框的用户名和密码

NSString *userNameStr = [alertController.textFields[0] text];

NSString *passwordStr = [alertController.textFields[1] text];

NSLog(@"userName is: %@  password is: %@", userNameStr, passwordStr);

}];

action;

})];

// 6. 添加输入框到alertView中,注意,actionSheet是没有办法添加textField的,强行添加会Crash

if (alertController.preferredStyle == UIAlertControllerStyleAlert) {

// 添加用户名输入框

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

// 给输入框设置一些信息

textField.placeholder = @"请输入用户名";

textField.textAlignment = NSTextAlignmentCenter;

}];

// 添加密码输入框

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

textField.placeholder = @"请输入密码";

textField.secureTextEntry = YES;

textField.textAlignment = NSTextAlignmentCenter;

}];

}

// 7. 显示(使用模态视图推出)

[self presentViewController:alertController animated:YES completion:nil];

}

 
 
// Demo下载地址:百度网盘

UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)的更多相关文章

  1. [Swift]UIKit学习之警告框:UIAlertController和UIAlertView

    Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) T ...

  2. IOS提示控件UIActionSheet,UIAlertView

    iphone中常用的消息提示控件,就是UIActionSheet和UIAlertView了,在Web开发中,UIActionSheet就像是confirm(),而UIAlertView就像是alert ...

  3. UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlert

    UIAlertController * cancleAlertController = [UIAlertController alertControllerWithTitle:nil message: ...

  4. iOS---stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead,

    旧方法 NSString *encoded = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; iOS9   ...

  5. UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0

  6. iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  7. iOS 8及以后版本 如何创建UIAlertView?

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  8. [New learn] UIKit 框架类

    NSObject NSObject is the root class of most Objective-C class hierarchies. NSDataAsset The NSDataAss ...

  9. iOS开发之UIAlertView与UIAlertController的详尽用法说明

    本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...

随机推荐

  1. Android开源项目发现--- 传感器篇(持续更新)

    Great Android Sensing Toolkit Android感应器工具包,包含示例及使用过程中可能需要的算法 项目地址:https://github.com/gast-lib/gast- ...

  2. win7 下与mac虚拟机的共享文件的建立

    1. 确保针对Mac虚拟机的VMware Tools的安装 加载进入系统后,在mac里可看到安装和卸载vmware tools的两个图标(点开vmware tools磁盘),点安装的就可以了. 2. ...

  3. Git fork指令

    ​ork并且更新一个仓库 现在有这样一种情形:有一个叫做Joe的程序猿写了一个游戏程序,而你可能要去改进它.并且Joe将他的代码放在了GitHub仓库上.下面是你要做的事情: fork并且更新GitH ...

  4. Lua常用的数据结构表示

    1.矩阵 Lua中有两种表示矩阵的方法,一是“数组的数组”.也就是说,table的每个元素是另一个table.例如,可以使用下面代码创建一个n行m列的矩阵:mt = {}          -- cr ...

  5. window.alert弹出处理

    # -*- coding:utf-8 -*- """ window.alert 处理 """ from selenium import we ...

  6. leetcode First Missing Positive hashset简单应用

    public class Solution { public int firstMissingPositive(int[] A) { HashSet<Integer> hash=new H ...

  7. 关于Marsedit和我的163博客

    其实我一开始选择的并不是163的博客,感觉没什么新意,勾不起我的兴趣. 第一个我想写的是CSDN的博客,代码嘛,总要找个专业的网站来写,但是我发现了一个及其让我不爽的事情,我竟然在网页上无法新建一篇日 ...

  8. COM初体验

    以前在我学校里培训过一段时间C++,我敬爱的吴老师略有提及.那个时候觉得COM遥不可及,觉得,哇塞好神圣.我觉得自己啥都没学好,我不应该这么早去涉及这片过于光荣的领地.既没有觉悟也没有动力去迎接这样一 ...

  9. HTTP协议的特点

    HTTP协议的主要特点可概括如下: 1.支持客户/服务器模式.2.简单快速:客户向服务器请求服务时,只需传送请求方法和路径.请求方法常用的有GET.HEAD.POST.每种方法规定了客户与服务器联系的 ...

  10. javaWeb上传文件代码

    javaweb两种方式的上传,1普通上传,2:jquery ajax后台上传,部分截图如下: 完成包下载,下载后倒入myeclipse工程即可,下载地址:http://files.cnblogs.co ...