IOS8 当前UIAlertView 改用 UIAlertController 实现模态窗和操作板。UIAlertController 的应用与UIAlerView 十分不同,它实际上是把弹窗内容与显示方式、按钮列表、拆散。实现起来非常简单。如下
1.调用静态方法创立弹窗控制器 alertControllerWithTitle
申明弹窗控制器,title
示意弹窗的题目,message
示意弹窗文字内容,重点是preferredStyle
示意弹窗的显示方式,UIAlertControllerStyleActionSheet
操作版形式显示,UIAlertControllerStyleAlert
模态窗形式
// 创立控制器 UIAlertController* alertConrtoll = [UIAlertController alertControllerWithTitle:@"谬误" message:@"网络谬误,获取失败" preferredStyle:UIAlertControllerStyleActionSheet];
2.为弹窗控制器减少按钮 UIAlertAction
UIAlertActions 是弹窗按钮类,通过静态方法actionWithTitle 创立,style
示意按钮格调,handler
是按钮被点击的回调函数。咱们创立完按钮组件通过 addAction
退出弹窗控制器
// 创立弹窗按钮组件 UIAlertAction* okBtn = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler: nil]; UIAlertAction* cancelBtn = [UIAlertAction actionWithTitle:@"从新获取" style:UIAlertActionStyleCancel handler: nil]; // 增加按钮 [alertConrtoll addAction:okBtn]; [alertConrtoll addAction:cancelBtn];
显示弹窗
显示弹窗和插入视图控制器办法统一。
[self presentViewController:alertConrtoll animated:YES completion:nil];
UIAlertController 属性
名称 | 类型 | 阐明 | 默认值 |
---|---|---|---|
title | NSString | 题目 | |
preferredStyle | UIAlertControllerStyle | 弹窗显示方式,只读 | |
actions | NSArray<UIAlertAction *> | 弹窗按钮列表,只读 |
UIAlertAction 属性
名称 | 类型 | 阐明 | 默认值 |
---|---|---|---|
enabled | BOOL | 是否启用 | |
title | NSString | 题目 | |
style | UIAlertActionStyle | 按钮格调 | UIAlertActionStyleDefault |
UIAlertController API
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
创立弹窗控制器并且设置题目,内容,显示格调- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler
增加可输出弹窗
UIAlertAction API
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler
创立弹窗按钮并且设置题目和格调、处理事件