iOS8中UIAlertController的使用

來源:互聯網
上載者:User

標籤:style   io   os   使用   sp   cti   on   c   log   

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

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

 UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

    

    //preferredStyle--唯讀

    //列印選擇的AlertController的類型 actionSheet--0 alertView--1

    NSLog(@"%ld",alertC.preferredStyle);

    //擷取alertC的標題和資訊

    NSLog(@"%@",alertC.title);

    NSLog(@"%@",alertC.message);

    //更改標題

    alertC.title = @"title change";

    

    //直接給alertC添加事件執行按鈕actionTitle(只能添加一個)

    /*

     [alertC addAction:[UIAlertAction actionWithTitle:@"actionTitle" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

     //點擊actionTitle時的回調方法

     }]];

     */

    

    //添加多個按鈕

    //沒有任何代理的情況下,在我們點擊alertView介面中的按鈕時,就不需要用buttonIndex來區分我們點擊的是哪個按鈕,同時一個介面上多個alertView時,也不需要用tag值來區分你到底點擊的是哪一個alertView上的按鈕了

    //這樣便於我們的邏輯思維,但是代碼量並沒有減少

    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")

                                                     style:UIAlertActionStyleCancel

                                                   handler:^(UIAlertAction *action) {

                                                       //cancle對應執行的代碼

                                                       NSLog(@"cancle action");

                                                   }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")

                                                      style:UIAlertActionStyleDefault

                                                    handler:^(UIAlertAction *action) {

                                                        //sure對應執行的代碼

                                                        NSLog(@"sure action");

                                                    }];

    

    [alertC addAction:action];

    [alertC addAction:action1];

    

    //alertView的輸入框 sheet類型中沒有 但是sheet類型的UIAlertController可以添加textField 當使用者要顯示sheet時程式會崩

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder = @"textFiled";

    }];

    

    //在這裡不管是alertView還是actionSheet都是一個獨立的Cotroller,所以這裡需要通過presentViewController來展現我們的alertView或者actionSheet

    [self presentViewController:alertC animated:YES completion:nil];

 

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

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")

                                                     style:UIAlertActionStyleCancel

                                                   handler:^(UIAlertAction *action) {

                                                       NSLog(@"cancle action");

                                                   }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")

                                                      style:UIAlertActionStyleDefault

                                                    handler:^(UIAlertAction *action) {

                                                        NSLog(@"sure action");

                                                    }];

    

    [alertC addAction:action];

    [alertC addAction:action1];

    [self presentViewController:alertC animated:YES completion:nil];

iOS8中UIAlertController的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.