在 iOS 8 中姨谷,UIAlertController 在功能上是和 UIAlertView 以及
UIActionSheet 相同的映九,UIAlertController以一種模塊化替換的方式來代替這兩個的功能和作用。
" UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead."
"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead. "
一件甥、UIAlertController的使用步驟
使用UIAlertController共需要三步:
實例化UIAlertController
實例化UIAlertAction
顯示UIAlertController
1、實例化UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
初始化時候需要設(shè)置樣式 UIAlertControllerStyle禁灼,來確定是 Alert 還是 ActionSheet:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
2轿曙、實例化UIAlertAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了Cancel");
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了OK");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
通過創(chuàng)建 UIAlertAction 的實例僻孝,你可以將動作按鈕添加到控制器上守谓。
UIAlertAction 由標題字符串、樣式以及當用戶選中該動作時運行的代碼塊組成斋荞。
為了實現(xiàn)原來我們在創(chuàng)建 UIAlertView 時創(chuàng)建的按鈕效果,我們只需創(chuàng)建這兩個動作按鈕并將它們添加到控制器上即可平酿。
通過 UIAlertActionStyle,您可以選擇如下三種動作樣式:常規(guī)(default)筑辨、取消(cancel)以及警示(destruective)幸逆。
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
// 常規(guī)樣式
UIAlertActionStyleDefault = 0,
// 取消樣式
UIAlertActionStyleCancel,
// 警示樣式,按鈕字體為紅色
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
按鈕顯示的次序取決于它們添加到對話框控制器上的次序还绘。一般來說,根據(jù)蘋果官方制定的《iOS 用戶界面指南》拍顷,在擁有兩個按鈕的對話框中,您應(yīng)當將取消按鈕放在左邊凭舶。要注意爱沟,取消按鈕是唯一的,如果您添加了第二個取消按鈕呼伸,那么你就會得到如下的一個運行時異常:
‘UIAlertController can only have one action with a style of UIAlertActionStyleCancel’
2.1钝尸、UIAlertActionStyleDefault 和 UIAlertActionStyleCancel 的區(qū)別:
添加UIAlertActionStyleCancel樣式后,在UIAlertAction只有兩個時珍促,UIAlertActionStyleCancel樣式的action都會按蘋果的默認風格把取消按鈕放在左邊;
添加UIAlertActionStyleCancel樣式后娇斩,在UIAlertAction有三個及其以上的時候,UIAlertActionStyleCancel樣式的action會顯示在最下面犬第。
而添加UIAlertActionStyleDefault樣式時,與你addAction到alertController上的順序有關(guān)歉嗓。
2.2、UIAlertActionStyleDestructive 警示樣式
警示樣式的按鈕變成了紅色哮幢。根據(jù)蘋果官方的定義志珍,“警示”樣式的按鈕是用在可能會改變或刪除數(shù)據(jù)的操作上,因此用了紅色的醒目標識來警示用戶碴裙。
3、顯示UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
主要代碼:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了Cancel");
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了OK");
}];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];