iOS8统一的系统提示控件——UIAlertController数组
1、引言atom
相信在iOS开发中,你们对UIAlertView和UIActionSheet必定不陌生,这两个控件在UI设计中发挥了很大的做用。然而若是你用过,你会发现这两个控件的设计思路有些繁琐,经过建立设置代理来进行界面的交互,将代码逻辑分割了,而且很容易造成冗余代码。在iOS8以后,系统吸引了UIAlertController这个类,整理了UIAlertView和UIActionSheet这两个控件,在iOS中,若是你扔使用UIAlertView和UIActionSheet,系统只是会提示你使用新的方法,iOS9中,这两个类被彻底弃用,但这并不说明旧的代码将不能使用,旧的代码依然能够工做很好,可是会存在隐患,UIAlertController,不只系统推荐,使用更加方便,结构也更加合理,做为开发者,使用新的警示控件,咱们何乐而不为呢。这里有旧的代码的使用方法:.net
UIAlertView使用:http://my.oschina.net/u/2340880/blog/408873。设计
UIActionSheet使用:http://my.oschina.net/u/2340880/blog/409907。代理
2、UIAlertController的使用blog
从这个类的名字咱们就能够看出,对于警示控件,设计的思路再也不是View而是Controller。经过present和push进行呼出,而不是之前的show方法。另外一个机制改变的地方是,其中按钮的触发方法再也不经过代理处理,而是将按钮封装成了类:UIAlertAction。详细方法及使用以下:开发
UIAlertController * con = [UIAlertController alertControllerWithTitle:@"新的" message:@"看看样子" preferredStyle:UIAlertControllerStyleAlert];get
[con addAction:[UIAlertAction actionWithTitle:@"仔细看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {it
//按钮触发的方法io
}]];
[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//警告的风格
}
风格效果以下:
3、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;
获取警示控件的风格