一、概述
iOS開發中,經常需要在app中使用彈窗提示用戶信息或者提供選擇操作,UIAlertController提供了一種簡單易用的方式來實現這些功能。UIAlertController可以在視圖控制器中以模態展示的形式展示警告框、操作表、文本框等。本文將詳細介紹UIAlertController的使用方法,以及如何自定義警告框的樣式和動作。
二、UIAlertController的基本用法
在iOS中,UIAlertController有兩個主要的類型:UIAlertControllerStyleAlert(警告框)和UIAlertControllerStyleActionSheet(操作表)。
(一)UIAlertControllerStyleAlert的使用
UIAlertControllerStyleAlert警告框的創建方法如下:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"提示內容" preferredStyle:UIAlertControllerStyleAlert];
其中,title參數可選,用於設置警告框的標題,message參數也可選,用於設置警告框的提示信息。
下面是創建UIAlertControllerStyleAlert的簡單示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否確認刪除?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//刪除操作
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];
如上代碼所示,首先創建了一個UIAlertController的實例,並設置了標題和提示內容,然後創建兩個UIAlertAction的實例,cancelAction表示“取消”操作,deleteAction表示“刪除”操作。addAction方法可以為UIAlertController添加操作,最後使用presentViewController:animated:completion:方法來展示UIAlertController。
運行上述代碼,會彈出一個警告框,如下圖所示:
(二)UIAlertControllerStyleActionSheet的使用
UIAlertControllerStyleActionSheet操作表的創建方式如下:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
其中,title和message都設置為nil,因為操作表沒有標題和提示信息。下面是創建UIAlertControllerStyleActionSheet的示例:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//拍照操作
}];
UIAlertAction *photoLibraryAction = [UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//從相冊選擇操作
}];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:cameraAction];
[actionSheetController addAction:photoLibraryAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
如上代碼所示,首先創建了一個UIAlertController的實例,然後添加了三個UIAlertAction的實例,cancelAction表示“取消”操作,cameraAction表示“拍照”操作,photoLibraryAction表示“從相冊選擇”操作,最後使用presentViewController:animated:completion:方法來展示UIAlertController。
運行上述代碼,會彈出一個操作表,如下圖所示:
三、自定義UIAlertController的外觀和操作
(一)自定義UIAlertController的外觀
UIAlertController提供了多種方式來自定義警告框和操作表的外觀,例如修改標題顏色和字體、修改按鈕顏色和字體等。下面是一個修改UIAlertControllerStyleAlert外觀的簡單示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否確認刪除?" preferredStyle:UIAlertControllerStyleAlert];
//修改標題顏色和字體
NSMutableAttributedString *titleAttributedStr = [[NSMutableAttributedString alloc] initWithString:@"提示" attributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
[alertController setValue:titleAttributedStr forKey:@"attributedTitle"];
//修改提示內容顏色和字體
NSMutableAttributedString *messageAttributedStr = [[NSMutableAttributedString alloc] initWithString:@"是否確認刪除?" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
[alertController setValue:messageAttributedStr forKey:@"attributedMessage"];
//修改“取消”按鈕的顏色和字體
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[alertController addAction:cancelAction];
//修改“刪除”按鈕的顏色和字體
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//刪除操作
}];
[deleteAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];
如上代碼所示,使用setValue:forKey:方法來修改UIAlertController的屬性值,其中key值是屬性名,value值是修改後的屬性值。運行上述代碼,會彈出一個外觀自定義的UIAlertController,如下圖所示:
(二)自定義UIAlertController的操作
除了可以自定義UIAlertController的外觀之外,還可以自定義UIAlertController的操作。UIAlertController提供了多種方式來處理按鈕點擊事件,例如執行一些代碼、跳轉頁面等。下面是一個自定義UIAlertController操作的簡單示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否確認刪除?" preferredStyle:UIAlertControllerStyleAlert];
//修改“取消”按鈕的顏色和字體
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[alertController addAction:cancelAction];
//修改“刪除”按鈕的顏色和字體
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//刪除操作
NSLog(@"刪除操作");
}];
[deleteAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
[alertController addAction:deleteAction];
//添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入密碼";
}];
[self presentViewController:alertController animated:YES completion:nil];
如上代碼所示,使用addAction方法來添加UIAlertAction的實例,使用addTextFieldWithConfigurationHandler方法來添加文本框。在刪除操作的handler中,可以執行想要的代碼,例如執行刪除操作,或是重新加載頁面。運行上述代碼,會彈出一個帶有文本框的警告框,並且在刪除操作時會打印一條日誌,如下圖所示:
四、總結
UIAlertController是一種簡單易用的方式來實現彈窗提示和操作選擇功能。它提供了兩種主要的類型:UIAlertControllerStyleAlert(警告框)和UIAlertControllerStyleActionSheet(操作表),並且可以通過修改屬性來自定義警告框和操作表的外觀樣式,也可以通過添加文本框或是自定義操作來擴展UIAlertController的功能。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/153409.html
微信掃一掃
支付寶掃一掃