提示框控制器:UIAlertController

提示框按钮:UIAlertAction
 
功能:用来提示信息,并给出一些可以进行选择的按钮来处理相应的要求。
 
注意:在Xcode的iOS8 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代。官方库解释: “UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.”、“UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead.” 。说明了在iOS8+开发,UIALertView和UIActionSheet已经过时了,UIAlertController以一种模块化替换的方式来代替这两这两个控件的功能和作用。如何创建及使用UIAlertController成为我们所关注的问题。
 
类介绍:
1、提示框风格枚举(分为UIAlertView、UIActionSheet)

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

UIAlertControllerStyleActionSheet = 0,  //在视图底部弹出的提示框,它不能添加文本框,而且在ipad中必须使用popover形式展示

UIAlertControllerStyleAlert                  //在视图中间弹出的提示框

} NS_ENUM_AVAILABLE_IOS(8_0);

2、提示框上按钮的风格

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

UIAlertActionStyleDefault = 0,    //默认的确认按钮

UIAlertActionStyleCancel,          //默认的取消按钮

UIAlertActionStyleDestructive    //默认的红色按钮

}NS_ENUM_AVAILABLE_IOS(8_0);

3、UIAlertController:提示框控制器类

@interface UIAlertController : UIViewController

方法:

//创建提示框控制器的类方法

+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

//在提示框上添加文本框的实例方法(只能在UIAlertView风格的提示框添加)

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

//在提示框上添加按钮

- (void)addAction:(UIAlertAction *)action;

属性:

//提示框上存放所有按钮的数组

@property (nonatomic, readonly) NSArray *actions;

//提示框上存放所有文本框的数组

@property (nonatomic, readonly) NSArray *textFields;

//提示框的标题

@property (nonatomic, copy) NSString *title;

//提示信息

@property (nonatomic, copy) NSString *message;

//提示框控制器的风格

@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;

@end

4、UIAlertAction:提示框按钮

@interface UIAlertAction : NSObject <NSCopying>

方法:

//创建提示框按钮的类方法

+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;

属性:

//按钮标题

@property (nonatomic, readonly) NSString *title;

//按钮的风格

@property (nonatomic, readonly) UIAlertActionStyle style;

//按钮是否有效

@property (nonatomic, getter=isEnabled) BOOL enabled;

@end

具体的实例如下:

创建步骤:

1、布局故事板,在控制器的视图中拖入一个按钮,并关联IBAction事件

2、在按钮的关联事件中的主要代码如下:

//创建提示框控制器

    //创建提示框控制器
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示框" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.backgroundColor = [UIColor purpleColor];

//创建提示框按钮

    //创建提示按钮
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"默认Cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"默认Default" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"默认Destructive" style:UIAlertActionStyleDestructive handler:nil];

//添加提示按钮到提示框中

    //添加提示按钮
[alertController addAction:action1];
[alertController addAction:action2];
[alertController addAction:action3];

//添加文本框到提示框中(只适合提示框风格为:UIAlertControllerStyleAlert)

    //添加文本框(只适合alertview类型的提示框)
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"账号";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES; //安全输入模式
}];

//给文本框添加监听事件

    //给文本框添加监听事件(文本框的开始、结束、状态改变等)
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"添加监听事件"; [textField addTarget:self action:@selector(alertTextFiledDidChanged:) forControlEvents:UIControlEventEditingChanged];
}];

//以模态窗口的形式显示提示框

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

//实现文本框事件

#pragma mark 文本框监听事件
-(void)alertTextFiledDidChanged:(NSNotification *)notification
{
NSLog(@"Enditing changed");
}

//点击按钮,显示演示结果

当没有添加action3按钮到提示框,即按钮个数<=2时,两种提示框的样式截图为:

UIAlertControllerStyleAlert:从屏幕中间弹出

UIAlertControllerStyleActionSheet:从屏幕底部弹出(不能添加文本框)

当没有添加action3按钮到提示框,即按钮个数>=3时,两种提示框的样式截图为:

UIAlertControllerStyleAlert:从屏幕中间弹出

UIAlertControllerStyleActionSheet:从屏幕底部弹出(不能添加文本框)

iOS:UIAlertController和UIAlertAction的详解的更多相关文章

  1. iOS 视图控制器转场详解

    iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...

  2. iOS 开发之照片框架详解(2)

    一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...

  3. iOS中MVC等设计模式详解

    iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...

  4. iOS 证书与签名 解惑详解

    iOS 证书与签名 解惑详解 分类: iPhone2012-06-06 19:57 9426人阅读 评论(1) 收藏 举报 iosxcodecryptographyappleiphone测试   目录 ...

  5. iOS 6分享列表——UIActivityViewController详解

    iOS 6分享列表——UIActivityViewController详解 2013-06-03 01:42:33     发表评论 在iOS 6之后提供了一个分享列表视图,它通过UIActivity ...

  6. IOS数据库操作SQLite3使用详解(转)

    iPhone中支持通过sqlite3来访问iPhone本地的数据库.具体使用方法如下1:添加开发包libsqlite3.0.dylib首先是设置项目文件,在项目中添加iPhone版的sqlite3的数 ...

  7. iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

    本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...

  8. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  9. iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...

随机推荐

  1. VBS基础篇 - 常用函数

    Option Explicit '*********************************Date/Time函数******************************* 'CDate函 ...

  2. 安装v2meet客户端 进入会议依然 提示 您还未安装视频会议的客户端,请下载安装

    解决办法 1.安装软件,要用管理员权限安装 2.装一个360浏览器,登录会议,这样就成功了.原装IE9却不行. 估计是IE9做了一些安全限制,由于时间关系就没有再处理了.

  3. 项目结队开发---NABC分析(成员)

    一.简介 项目名称:校园导航 特点:手机app,简便易用,适合对铁大地形不了解.路痴者使用. 二.NABC分析 N(need):对于新生报到,学生家长参观校园等想要了解校园路线者,本app软件将带给你 ...

  4. Java面试之SE基础基本数据类型

    1.九种基本数据类型的大小以及它们的封装类 在我们面试或者考试过程中经常会考到八种基本数据类型以及它们的封装类,那么有哪八种基本数据类型呢?它们的封装类又是什么呢? 首先,八种基本数据类型分别是:in ...

  5. SQL-AdventureWorks样例数据库

    1. 下载样例数据库文件 输入网址:http://www.codeplex.com/ , 搜索:microsoft sql server product samples 下载对应数据库的Adventu ...

  6. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  7. DFS-HDU 1312 -Red and Black

    D - Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. C#--简单的串口通信程序

    前几天做毕业设计,其中要用到串口和下位机进行通信,于是自己捣鼓了一个简单的串口通信程序. 在做通信之前要先弄一个SerialPort组件出来,当然也可以通过程序来创建.本次设计中采用的是拖的winfo ...

  9. GS界面上显示的重要参考数据

    GS界面上显示的重要参考数据,这个是压测时重要参考 struct GSinfo { int revBuffNum; int sendBuffNum; int clientNum; int dbAskN ...

  10. 安装wine qq2012

    添加软件源:vi /etc/apt/sources.list deb http://http.kali.org/kali kali main non-free contribdeb-src http: ...