IOS SDK詳解之UIAlertController(IOS8之後替代AlertView和ActionSheet)

來源:互聯網
上載者:User

標籤:ios   alert   sheet   action   

原創Blog,轉載請註明出處
blog.csdn.net/hello_hwc

前言:有兩個月左右沒為公司開發IOS項目了(最近一直在搞IOT),以至於對IOS 8的這個更新都沒看到。這裡補上。

一 概述

在IOS8之後,UIAlertController替代了UIActionSheet和UIAlertView。把兩種類型的提示資訊放到這一個類裡來實現。
注意, 這個class不能通過繼承的方式來自訂

二 類介紹

先舉兩個使用的例子
例子一

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil                                                                             message: nil                                                                       preferredStyle:UIAlertControllerStyleActionSheet];    //添加Button    [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        //處理點擊拍照    }]];    [alertController addAction: [UIAlertAction actionWithTitle: @"從相簿選取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){        //處理點擊從相簿選取    }]];    [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];    [self presentViewController: alertController animated: YES completion: nil];

例子二

實現代碼

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"登陸"                                                                                  message: @"輸入使用者名稱密碼"                                                                              preferredStyle:UIAlertControllerStyleAlert];    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        textField.placeholder = @"name";        textField.textColor = [UIColor blueColor];        textField.clearButtonMode = UITextFieldViewModeWhileEditing;        textField.borderStyle = UITextBorderStyleRoundedRect;    }];    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        textField.placeholder = @"password";        textField.textColor = [UIColor blueColor];        textField.clearButtonMode = UITextFieldViewModeWhileEditing;        textField.borderStyle = UITextBorderStyleRoundedRect;        textField.secureTextEntry = YES;    }];    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSArray * textfields = alertController.textFields;        UITextField * namefield = textfields[0];        UITextField * passwordfiled = textfields[1];        NSLog(@"%@:%@",namefield.text,passwordfiled.text);    }]];    [self presentViewController:alertController animated:YES completion:nil];
三 使用的步驟

第一步 初始化

+ (instancetype)alertControllerWithTitle:(NSString *)title                                 message:(NSString *)message                          preferredStyle:(UIAlertControllerStyle)preferredStyle

這裡的preferredStyle有兩種,sheet和alert

typedef enum UIAlertControllerStyle: NSInteger {   UIAlertControllerStyleActionSheet = 0,   UIAlertControllerStyleAlert } UIAlertControllerStyle;

第二步,添加Action(button或者textfield)
添加Button
- (void)addAction:(UIAlertAction *)action
這裡的UIAlertAction是一個比較簡單的類

+ (instancetype)actionWithTitle:(NSString *)title                          style:(UIAlertActionStyle)style                        handler:(void (^)(UIAlertAction *action))handler

style有三種

typedef enum UIAlertActionStyle: NSInteger {   UIAlertActionStyleDefault = 0,//預設   UIAlertActionStyleCancel,//取消   UIAlertActionStyleDestructive //有可能改變或者資料} UIAlertActionStyle;

添加TextField
注意,只能是 UIAlertControllerStyleAlert才能添加Textfield

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler

在block裡配置textfield的資訊,例如placeholder,backgroundcolor等。
Textfield的儲存資訊可由UIAlertController的屬性Textfields獲得。如同上述的例子二一樣。

第三步,顯示
例如

[self presentViewController:alert animated:YES completion:nil];
總結:

總的來說,API把兩種Alertview進行統一,並且不用代理的方式來傳遞訊息。而是改成了block。就個人而言,還是比較喜歡Block的方式的。上述的兩幅圖的背景請忽略,那是我準備寫另一篇關於拍照部落格的Demo工程。

IOS SDK詳解之UIAlertController(IOS8之後替代AlertView和ActionSheet)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.