对话框的简单使用
创建一个UIAlertController对象
- 使用UIAlertController自己的便利构造器
+ (instancetype)alertControllerWithTitle:(NSString *)title message:
(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
- title 代表对话框的标题
- message 代表对话框提示的内容
- preferredStyle 代表弹出对话框的风格 一共有两种 UIAlertControllerStyleActionSheet 和 UIAlertControllerStyleAlert
- UIAlertControllerStyleActionSheet表示对话框在屏幕底部
- UIAlertControllerStyleAlert表示对话框在屏幕中央
UIAlertController *alert = [UIAlertController alertControllerWithTitle:
@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];
创建两个UIAlertAction对象(相当于对话框下面的按钮)
+ (instancetype)actionWithTitle:(nullable NSString *)
style:(UIAlertActionStyle) handler:^(UIAlertAction * _Nonnull action)handler
- style要显示的按钮风格
- block中写当点击按钮时要执行的操作(可以不写)
UIAlertAction *action = [UIAlertAction actionWithTitle:@"NO"
style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"测试" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
把创建的UIAlertAction对象添加到alert上
[alert addAction:action];
[alert addAction:action2];
[alert addAction:action3];
模态推入对话框
[self presentViewController:alert animated:YES completion:^{
}];
风格为UIAlertControllerStyleAlert时
- 当一个UIAlertController上的action个数超过两个时 竖着排列 如下图

QQ20160625-0.png
- 当一个UIAlertController上的action个数不超过两个时 横着排列 如下图
- 注释掉其中一个UIAlertAction

QQ20160625-1.png
更改alert的风格 把风格改为UIAlertControllerStyleActionSheet

QQ20160625-2.png
- 风格为UIAlertControllerStyleActionSheet alert上不管添加几个UIAlertAction都是竖着排列

QQ20160625-3.png
- action的个数由需求决定, 需要几个action就添加几个
网友评论