iOS8統一的系統提示控件——UIAlertController
一、引言
相信在iOS開發中,大家對UIAlertView和UIActionSheet一定不陌生,這兩個控件在UI設計中發揮了很大的作用。然而如果你用過,你會發現這兩個控件的設計思路有些繁瑣,通過創建設置代理來進行界面的交互,將代碼邏輯分割了,並且很容易形成冗餘代碼。在iOS8之後,系統吸引了UIAlertController這個類,整理了UIAlertView和UIActionSheet這兩個控件,在iOS中,如果你扔使用UIAlertView和UIActionSheet,系統只是會提示你使用新的方法,iOS9中,這兩個類被完全棄用,但這並不說明舊的代碼將不能使用,舊的代碼依然可以工作很好,但是會存在隱患,UIAlertController,不僅系統推薦,使用更加方便,結構也更加合理,作爲開發者,使用新的警示控件,我們何樂而不爲呢。這裏有舊的代碼的使用方法:
UIAlertView使用:http://my.oschina.net/u/2340880/blog/408873。
UIActionSheet使用:http://my.oschina.net/u/2340880/blog/409907。
二、UIAlertController的使用
從這個類的名字我們就可以看出,對於警示控件,設計的思路不再是View而是Controller。通過present和push進行呼出,而不是以前的show方法。另一個機制改變的地方是,其中按鈕的觸發方法不再通過代理處理,而是將按鈕封裝成了類:UIAlertAction。詳細方法及使用如下:
1 2 3 4 5 | UIAlertController * con = [UIAlertController alertControllerWithTitle:@ "新的" message:@ "看看樣子" preferredStyle:UIAlertControllerStyleAlert]; [con addAction:[UIAlertAction actionWithTitle:@ "仔細看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { //按鈕觸發的方法 }]]; [self presentViewController:con animated:YES completion:nil]; |
上面的代碼,會在屏幕上呼出警告框,如下:
初始化方法中的preferref參數是一個枚舉,決定是提示框或者抽屜列表:
1 2 3 4 | typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, //抽屜 UIAlertControllerStyleAlert //警告框 } |
上面的addAction方法添加了一個封裝了方法的按鈕,UIAlertAction類的構造十分簡單,如下:
1 2 3 4 5 6 7 8 | //初始化方法 + (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:( void (^ __nullable)(UIAlertAction *action))handler; //獲取標題 @property (nullable, nonatomic, readonly) NSString *title; //獲取風格 @property (nonatomic, readonly) UIAlertActionStyle style; //設置是否有效 @property (nonatomic, getter=isEnabled) BOOL enabled; |
AlertAction的風格是如下的枚舉:
1 2 3 4 5 | typedef NS_ENUM(NSInteger, UIAlertActionStyle) { UIAlertActionStyleDefault = 0, //默認的風格 UIAlertActionStyleCancel, //取消按鈕的風格 UIAlertActionStyleDestructive //警告的風格 } |
風格效果如下:
三、UIAlertController其他屬性和方法
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
獲取所有AlertAction
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
iOS9後新增加的屬性,可以使某個按鈕更加突出,只能設置已經在actions數組中的AkertAction,會使設置的按鈕更加顯眼,如下:
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
添加一個textField,以前的相關控件,雖然也可以添加textField,但是定製化能力非常差,這個新的方法中有一個configurationHandler代碼塊,可以將textField的相關設置代碼放入這個代碼塊中,並且這個方法添加的textField個數不再限制於2個:
1 2 3 4 5 6 7 8 9 | [con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder=@ "第1個" ; }]; [con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder=@ "第2個" ; }]; [con addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder=@ "第3個" ; }]; |
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
獲取所有textField的數組
@property (nullable, nonatomic, copy) NSString *title;
設置警示控件的標題
@property (nullable, nonatomic, copy) NSString *message;
設置警示控件的信息
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
獲取警示控件的風格
該文章轉自OSChina