UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)
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];
}
UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)的更多相关文章
- [Swift]UIKit学习之警告框:UIAlertController和UIAlertView
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) T ...
- IOS提示控件UIActionSheet,UIAlertView
iphone中常用的消息提示控件,就是UIActionSheet和UIAlertView了,在Web开发中,UIActionSheet就像是confirm(),而UIAlertView就像是alert ...
- UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlert
UIAlertController * cancleAlertController = [UIAlertController alertControllerWithTitle:nil message: ...
- iOS---stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead,
旧方法 NSString *encoded = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; iOS9 ...
- UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0
- iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建
1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...
- iOS 8及以后版本 如何创建UIAlertView?
1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...
- [New learn] UIKit 框架类
NSObject NSObject is the root class of most Objective-C class hierarchies. NSDataAsset The NSDataAss ...
- iOS开发之UIAlertView与UIAlertController的详尽用法说明
本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...
随机推荐
- Java实现字符串反转
替换原则:index k 的值和 n-k 的值进行交换.(始终记住程序员的n.k都是字符串的实际位置.) 乘除的最基本实现还是来源于移位操作. public String reverse(String ...
- Android网络框架Volley(实战篇)
之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...
- 参数化时按行读取txt文件,如何去掉换行符"\n"
参数化按行读取txt,每行默认自带了回车换行操作,导致脚本报错,故而按行读取时,需要去掉默认的'\n' #coding=utf-8 from selenium import webdriver imp ...
- POJ 1503 Integer Inquiry 简单大数相加
Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his explo ...
- valuestack(值栈) 和 actioncontext(上下文)
Strut2的Action类通过属性可以获得所有相关的值,如请求参数属性值等.要获得这些参数值,我们要做的唯一一件事就是在Action类中声明与参数同名的属性.在Struts2调用Action类的Ac ...
- 提高效率 常用的几个xcode快捷键
能用好快捷键,不仅仅可以提高工作效率,而且让你看起来更加的自信和能干,下面几个常用的快捷键,希望对你在工作中有帮助 1.首先说明一下几个标示的意思 Command ⌘ Control ⌃ ...
- XML文件操作指南
一.XML简介 XML的全名是eXtensible Markup Language(可以扩展的标记语言),它的语法类似HTML,都是用标签来描述数据.HTML的标签是固定的,我们只能使用.不能修改: ...
- conv2用法
1.用法 C=conv2(A,B,shape); %卷积滤波 A:输入图像,B:卷积核 假设输入图像A大小为ma x na,卷积核B大小为mb x nb,则 当sha ...
- usb转串口如何配置?
概述 USB转串口输出,在kernel启动阶段加载相应的usb转串口芯片驱动,加载成功后,可通过usb转串口与pc机端交互. 步骤 1. 在kernel配置中选中usb转串口驱动: 2. 传给内核 ...
- Java+protobuf 实例
之前开发都是JSON格式, 据说用这种格式的 安全, 输入输出全是二进制文件,且 数据占用内存小. 主要永远移动端数据传送.以下是代码: controller: 请求的是实体: package cn. ...