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];
相關文章
相關標籤/搜索