iOS:UIAlertController和UIAlertAction的详解

简介:

提示框控制器: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初级

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4889611.html,如需转载请自行联系原作者
相关文章
|
iOS开发 数据安全/隐私保护 Swift
iOS - UIAlertController
前言 NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController @available(iOS 8.
1186 0
|
Android开发 iOS开发
iOS 8 引入的 UIActionSheet 和 UIAlertView 的替代品 - UIAlertController
iOS 8 引入的 UIActionSheet 的替代品 - UIAlertController 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1110 0
iOS8开发之iOS8的UIAlertController
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSh
1251 0
|
30天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
84 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
91 2
|
5天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
9 0