UIAlertController的使用
2016-07-23 00:00
465 查看
一、UIAlertController的使用
UIAlertController * con = [UIAlertController alertControllerWithTitle:@"新的" message:@"看看样子" preferredStyle:UIAlertControllerStyleAlert];
[con addAction:[UIAlertAction actionWithTitle:@"仔细看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//按钮触发的方法
}]];
[self presentViewController:con animated:YES completion:nil];
上面的代码,会在屏幕上呼出警告框,如下:
初始化方法中的preferref参数是一个枚举,决定是提示框或者抽屉列表:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,//抽屉
UIAlertControllerStyleAlert//警告框
}
上面的addAction方法添加了一个封装了方法的按钮,UIAlertAction类的构造十分简单,如下:
//初始化方法
(instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
//获取标题
@property (nullable, nonatomic, readonly) NSString *title;
//获取风格
@property (nonatomic, readonly) UIAlertActionStyle style;
//设置是否有效
@property (nonatomic, getter=isEnabled) BOOL enabled;
AlertAction的风格是如下的枚举:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,//默认的风格
UIAlertActionStyleCancel,//取消按钮的风格
UIAlertActionStyleDestructive//警告的风格
}
二、UIAlertController其他属性和方法
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
获取所有AlertAction
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
iOS9后新增加的属性,可以使某个按钮更加突出,只能设置已经在actions数组中的AkertAction,会使设置的按钮更加显眼,如下:
(void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
添加一个textField,以前的相关控件,虽然也可以添加textField,但是定制化能力非常差,这个新的方法中有一个configurationHandler代码块,可以将textField的相关设置代码放入这个代码块中,并且这个方法添加的textField个数不再限制于2个:
[con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"第1个";
}];
[con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"第2个";
}];
[con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"第3个";
}];
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
获取所有textField的数组
@property (nullable, nonatomic, copy) NSString *title;
设置警示控件的标题
@property (nullable, nonatomic, copy) NSString *message;
设置警示控件的信息
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
获取警示控件的风格
从这个类的名字我们就可以看出,对于警示控件,设计的思路不再是View而是Controller。通过present和push进行呼出,而不是以前的show方法。另一个机制改变的地方是,其中按钮的触发方法不再通过代理处理,而是将按钮封装成了类:UIAlertAction。详细方法及使用如下:
UIAlertController * con = [UIAlertController alertControllerWithTitle:@"新的" message:@"看看样子" preferredStyle:UIAlertControllerStyleAlert];
[con addAction:[UIAlertAction actionWithTitle:@"仔细看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//按钮触发的方法
}]];
[self presentViewController:con animated:YES completion:nil];
上面的代码,会在屏幕上呼出警告框,如下:
初始化方法中的preferref参数是一个枚举,决定是提示框或者抽屉列表:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,//抽屉
UIAlertControllerStyleAlert//警告框
}
上面的addAction方法添加了一个封装了方法的按钮,UIAlertAction类的构造十分简单,如下:
//初始化方法
(instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
//获取标题
@property (nullable, nonatomic, readonly) NSString *title;
//获取风格
@property (nonatomic, readonly) UIAlertActionStyle style;
//设置是否有效
@property (nonatomic, getter=isEnabled) BOOL enabled;
AlertAction的风格是如下的枚举:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,//默认的风格
UIAlertActionStyleCancel,//取消按钮的风格
UIAlertActionStyleDestructive//警告的风格
}
二、UIAlertController其他属性和方法
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
获取所有AlertAction
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
iOS9后新增加的属性,可以使某个按钮更加突出,只能设置已经在actions数组中的AkertAction,会使设置的按钮更加显眼,如下:
(void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
添加一个textField,以前的相关控件,虽然也可以添加textField,但是定制化能力非常差,这个新的方法中有一个configurationHandler代码块,可以将textField的相关设置代码放入这个代码块中,并且这个方法添加的textField个数不再限制于2个:
[con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"第1个";
}];
[con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"第2个";
}];
[con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"第3个";
}];
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
获取所有textField的数组
@property (nullable, nonatomic, copy) NSString *title;
设置警示控件的标题
@property (nullable, nonatomic, copy) NSString *message;
设置警示控件的信息
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
获取警示控件的风格
相关文章推荐
- 【STL记录】Containers--Deques
- [LeetCode] Wiggle Subsequence 摆动子序列
- 2016ACM暑假集训 - 4 Values whose Sum is 0
- 【Leetcode】Wiggle Subsequence
- 51.In a system, large online transaction processing (OLTP) jobs run during the daytime that require
- MUI(4)
- iOS开发 iOS7显示偏差(UITableView下移)解决办法
- UIWindow
- requireJS 用法
- UVA 11538 Chess Queen
- 适配ios7的时候仿UIAlertView弹框效果,修改内部的label和button的属性
- UICollectionViewCell 3种创建方法
- Biee 迁移和刷新GUIDs
- GUI为什么不设计为多线程(用户事件和底层事件的流程是相反的,每层都加锁效率太低,共用一把锁那就是单线程)
- codeforces 691E Xor-sequences(矩阵快速幂)
- 【POJ 2533】Longest Ordered Subsequence(最长上升子序列LIS)
- cpuinfo和lscpu查看CPU相关的信息
- EasyUI ComboBox(下拉列表框)
- POJ 1947 Rebuilding Roads (树形dp + 01背包)
- UICollectionViewCell中cell的定义与自定义方式总结