UIAlertController的使用

代码如下:

1
2
3
4
5
6
7
8
9
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

UIAlertControllerStyle

其中 UIAlertControllerStyle 枚举效果如下:

  • UIAlertControllerStyleAlert

  • UIAlertControllerStyleActionSheet

UIAlertActionStyle

  • UIAlertActionStyleDefault
  • UIAlertActionStyleCancel
  • UIAlertActionStyleDestructive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert message" message:@"This is an alert" preferredStyle:(UIAlertControllerStyleActionSheet)];

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:defaultAction];
[alert addAction:cancelAction];
[alert addAction:destructiveAction];

[self presentViewController:alert animated:YES completion:^{

}];

UIAlertActionStyleCancel 类型的 UIAlertAction 对象只能有一个,其他类型的 UIAlertAction 对象可以有多个

文章作者: Ammar
文章链接: http://lizhaoloveit.cn/2015/10/20/UIAlertController%E7%9A%84%E4%BD%BF%E7%94%A8/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ammar's Blog
打赏
  • 微信
  • 支付宝

评论