iOS8中UIAlertController的使用

iOS8中的UIAlertController包括了之前的UIAlertView和UIActionSheet,将它们集合成了自己的style;

1------------UIAlertControllerStyleAlert-----原有的UIAlertView 

 1   UIAlertController *alertC = [UIAlertController alertControllerWithTitle: @" alertC " message: @" message " preferredStyle:UIAlertControllerStyleAlert];
 2     
 3      // preferredStyle--只读
 4       // 打印选择的AlertController的类型 actionSheet--0 alertView--1
 5      NSLog( @" %ld ",alertC.preferredStyle);
 6      // 获取alertC的标题和信息
 7      NSLog( @" %@ ",alertC.title);
 8     NSLog( @" %@ ",alertC.message);
 9      // 更改标题
10      alertC.title =  @" title change ";
11     
12      // 直接给alertC添加事件执行按钮actionTitle(只能添加一个)
13       /*
14       [alertC addAction:[UIAlertAction actionWithTitle:@"actionTitle" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
15       //点击actionTitle时的回调方法
16       }]];
17        */
18     
19      // 添加多个按钮
20       // 没有任何代理的情况下,在我们点击alertView界面中的按钮时,就不需要用buttonIndex来区分我们点击的是哪个按钮,同时一个界面上多个alertView时,也不需要用tag值来区分你到底点击的是哪一个alertView上的按钮了
21       // 这样便于我们的逻辑思维,但是代码量并没有减少
22      UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString( @" cancle "@" Cancle action ")
23                                                      style:UIAlertActionStyleCancel
24                                                    handler:^(UIAlertAction *action) {
25                                                         // cancle对应执行的代码
26                                                         NSLog( @" cancle action ");
27                                                    }];
28     UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString( @" sure "@" Sure action ")
29                                                       style:UIAlertActionStyleDefault
30                                                     handler:^(UIAlertAction *action) {
31                                                          // sure对应执行的代码
32                                                          NSLog( @" sure action ");
33                                                     }];
34     
35     [alertC addAction:action];
36     [alertC addAction:action1];
37     
38      // alertView的输入框 sheet类型中没有 但是sheet类型的UIAlertController可以添加textField 当用户要显示sheet时程序会崩
39      [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
40         textField.placeholder =  @" textFiled ";
41     }];
42     
43      // 在这里不管是alertView还是actionSheet都是一个独立的Cotroller,所以这里需要通过presentViewController来展现我们的alertView或者actionSheet
44      [self presentViewController:alertC animated:YES completion:nil];

  

2---------------UIAlertControllerStyleActionSheet----原有的UIActionSheet

 1 UIAlertController *alertC = [UIAlertController alertControllerWithTitle: @" alertC " message: @" message " preferredStyle:UIAlertControllerStyleActionSheet];
 2     
 3     UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString( @" cancle "@" Cancle action ")
 4                                                      style:UIAlertActionStyleCancel
 5                                                    handler:^(UIAlertAction *action) {
 6                                                        NSLog( @" cancle action ");
 7                                                    }];
 8     UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString( @" sure "@" Sure action ")
 9                                                       style:UIAlertActionStyleDefault
10                                                     handler:^(UIAlertAction *action) {
11                                                         NSLog( @" sure action ");
12                                                     }];
13     
14     [alertC addAction:action];
15     [alertC addAction:action1];
16     [self presentViewController:alertC animated:YES completion:nil];

转载于:https://www.cnblogs.com/NINIiOS/p/3992247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值