iOS8中UIAlertController的使用

iOS8中的UIAlertController包括了以前的UIAlertView和UIActionSheet,将它们集合成了本身的style;css

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

 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----原有的UIActionSheetspa

 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];
相关文章
相关标签/搜索