iOS:UIAlertController和UIAlertAction的详解
提示框控制器:UIAlertController
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0, //在视图底部弹出的提示框,它不能添加文本框,而且在ipad中必须使用popover形式展示
UIAlertControllerStyleAlert //在视图中间弹出的提示框
} NS_ENUM_AVAILABLE_IOS(8_0);
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的详解的更多相关文章
- iOS 视图控制器转场详解
iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...
- iOS 开发之照片框架详解(2)
一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...
- iOS中MVC等设计模式详解
iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...
- iOS 证书与签名 解惑详解
iOS 证书与签名 解惑详解 分类: iPhone2012-06-06 19:57 9426人阅读 评论(1) 收藏 举报 iosxcodecryptographyappleiphone测试 目录 ...
- iOS 6分享列表——UIActivityViewController详解
iOS 6分享列表——UIActivityViewController详解 2013-06-03 01:42:33 发表评论 在iOS 6之后提供了一个分享列表视图,它通过UIActivity ...
- IOS数据库操作SQLite3使用详解(转)
iPhone中支持通过sqlite3来访问iPhone本地的数据库.具体使用方法如下1:添加开发包libsqlite3.0.dylib首先是设置项目文件,在项目中添加iPhone版的sqlite3的数 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)
本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...
- iOS 开发之照片框架详解
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...
随机推荐
- Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...
- 学习Linux第七天
1.shell echo $HOME 默认在shell中编写的变量全部是局部变量,如果重新打开console的话,那么这些变量将全部丢失,全局的变量可以写在文件~/.bashrc文件. 2.判断 !/ ...
- 来自平时工作中的javascript知识的积累---持续补充中
① SeaJs和RequireJS最大的区别 解惑:来自豆友 ② javascript中如何判断undefined var exp = undefined; if (exp === undefined ...
- if in hlsl
seems that in HLSL_4, we can use if https://msdn.microsoft.com/en-us/library/bb313972(v=xnagamestudi ...
- CREATEINPUTLAYOUT_INCOMPATIBLEFORMAT
这个error的全称是这样的 D3D11 ERROR: ID3D11Device::CreateInputLayout: Element[1]'s format (UNKNOW) cannot be ...
- [百度空间] [转]内存屏障 - MemoryBarrier
处理器的乱序和并发执行 目前的高级处理器,为了提高内部逻辑元件的利用率以提高运行速度,通常会采用多指令发射.乱序执行等各种措施.现在普遍使用的一些超标量处理器通常能够在一个指令周期内并发执行多条指令. ...
- linux 访问ntfs分区
打开ntfs-3g的下载点http://www.tuxera.com/community/ntfs-3g-download/ ,将最新稳定(当前最新版本为ntfs-3g-2011.1.15)下载到Ce ...
- UML建模类型(转载)
区分UML模型, UML建模用于不同类型的不同的图.有三个重要类型的UML建模: 结构建模: 系统结构建模捕捉静态功能.它们包括下列各项: 类图 对象图 部署图 包图 复合结构图 组件图 结构模型代表 ...
- Working with LOBs in Oracle and PHP
原文链接:http://www.oracle.com/technetwork/articles/fuecks-lobs-095315.html Working with LOBs in Oracle ...
- leetcode single number系列
这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数. 第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数 第三题是一组数里除了一个数出现一次外, ...