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 属性
名称类型阐明默认值
titleNSString题目
preferredStyleUIAlertControllerStyle弹窗显示方式,只读
actionsNSArray<UIAlertAction *>弹窗按钮列表,只读
UIAlertAction 属性
名称类型阐明默认值
enabledBOOL是否启用
titleNSString题目
styleUIAlertActionStyle按钮格调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 创立弹窗按钮并且设置题目和格调、处理事件