IOS8 之后UIAlertView 改用 UIAlertController 实现模态窗和操做板。UIAlertController 的使用与UIAlerView 很是不一样,它其实是把弹窗内容与显示方式、按钮列表、分离。实现起来很是简单。以下
声明弹窗控制器,title
表示弹窗的标题,message
表示弹窗文字内容,重点是preferredStyle
表示弹窗的显示方式,UIAlertControllerStyleActionSheet
操做版方式显示,UIAlertControllerStyleAlert
模态窗方式网络
// 建立控制器 UIAlertController* alertConrtoll = [UIAlertController alertControllerWithTitle:@"错误" message:@"网络错误,获取失败" preferredStyle:UIAlertControllerStyleActionSheet];
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];
显示弹窗和插入视图控制器方法一致。code
[self presentViewController:alertConrtoll animated:YES completion:nil];
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
title | NSString | 标题 | |
preferredStyle | UIAlertControllerStyle | 弹窗显示方式,只读 | |
actions | NSArray<UIAlertAction *> | 弹窗按钮列表,只读 |
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
enabled | BOOL | 是否启用 | |
title | NSString | 标题 | |
style | UIAlertActionStyle | 按钮风格 | UIAlertActionStyleDefault |
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
建立弹窗控制器而且设置标题,内容,显示风格- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler
添加可输入弹窗+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler
建立弹窗按钮而且设置标题和风格、处理事件