iOS UIAlertController實現彈窗提示和選擇功能

一、概述

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-tw/n/153409.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-14 03:05
下一篇 2024-11-14 03:05

相關推薦

  • 如何解決WPS保存提示會導致宏不可用的問題

    如果您使用過WPS,可能會碰到在保存的時候提示「文件中含有宏,保存將導致宏不可用」的問題。這個問題是因為WPS在默認情況下不允許保存帶有宏的文件,為了解決這個問題,本篇文章將從多個…

    編程 2025-04-29
  • Java和Python哪個功能更好

    對於Java和Python這兩種編程語言,究竟哪一種更好?這個問題並沒有一個簡單的答案。下面我將從多個方面來對Java和Python進行比較,幫助讀者了解它們的優勢和劣勢,以便選擇…

    編程 2025-04-29
  • 金融閱讀器提示配置文件無法識別

    在使用金融閱讀器過程中,有時會遇到提示配置文件無法識別的情況。這種情況通常是由於配置文件中存在錯誤或不完整所導致的。本文將從多個方面對此問題進行詳細的闡述,並提供相應解決方法。 一…

    編程 2025-04-28
  • Python每次運行變數加一:實現計數器功能

    Python編程語言中,每次執行程序都需要定義變數,而在實際開發中常常需要對變數進行計數或者累加操作,這時就需要了解如何在Python中實現計數器功能。本文將從以下幾個方面詳細講解…

    編程 2025-04-28
  • Python strip()函數的功能和用法用法介紹

    Python的strip()函數用於刪除字元串開頭和結尾的空格,包括\n、\t等字元。本篇文章將從用法、功能以及與其他函數的比較等多個方面對strip()函數進行詳細講解。 一、基…

    編程 2025-04-28
  • 全能的wpitl實現各種功能的代碼示例

    wpitl是一款強大、靈活、易於使用的編程工具,可以實現各種功能。下面將從多個方面對wpitl進行詳細的闡述,每個方面都會列舉2~3個代碼示例。 一、文件操作 1、讀取文件 fil…

    編程 2025-04-27
  • iOS開發如何添加許可權

    在iOS開發中,為了保護用戶的隱私和安全,應用程序可能需要請求一些許可權。 一、請求應用程序許可權 應用程序不得在用戶未給予許可的情況下獲取用戶數據。許多iOS系統功能都需要獲得用戶的…

    編程 2025-04-27
  • SOXER: 提供全面的音頻處理功能的命令行工具

    SOXER是一個命令行工具,提供了強大、靈活、全面的音頻處理功能。同時,SOXER也是一個跨平台的工具,支持在多個操作系統下使用。在本文中,我們將深入了解SOXER這個工具,並探討…

    編程 2025-04-27
  • uniapp ios打包詳解

    一、環境搭建 首先需要安裝Xcode,並在Xcode中登錄自己的Apple ID,開啟自己的開發者賬戶。 接著,需要在uniapp項目中配置簽名證書和描述文件。步驟如下: 在Xco…

    編程 2025-04-25
  • nobranchesreadyforupload功能詳解

    nobranchesreadyforupload是一個Git自動化工具,能夠在本地Git存儲庫中查找未提交的更改並提交到指定的分支。 一、檢查新建文件是否被提交 Git存儲庫中可能…

    編程 2025-04-25

發表回復

登錄後才能評論