其他分享
首页 > 其他分享> > IOS 模态弹窗与操作版使用 UIAlertController

IOS 模态弹窗与操作版使用 UIAlertController

作者:互联网

转:

IOS 模态弹窗与操作版使用 UIAlertController

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 属性
名称类型说明默认值
enabledBOOL是否启用
titleNSString标题
styleUIAlertActionStyle按钮风格UIAlertActionStyleDefault
UIAlertController API
UIAlertAction API

转:

IOS 模态弹窗与操作版使用 UIAlertController

标签:控制器,UIAlertAction,IOS,style,按钮,UIAlertController,弹窗
来源: https://www.cnblogs.com/wangtcc/p/14724034.html