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/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

发表回复

登录后才能评论