iOS开发 提示框UIAlertController的略微封装

2024-03-05 04:18

本文主要是介绍iOS开发 提示框UIAlertController的略微封装,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前写的代码,把UIAlertView的封装剔除之后,发现UIAlertController 封装的意义不是很大了,毕竟苹果公司封装的已经够好了,好了,上代码

//
//  XSDAlertViewTools.h
//  XSDSH  提示框工具类
//
//  Created by 小广 on 16/1/11.
//  Copyright © 2016年 XSD. All rights reserved.
//#import <Foundation/Foundation.h>#define cancelIndex    (-1)typedef void(^AlertViewBlock)(NSInteger buttonTag);@interface XSDAlertViewTools : NSObject+ (XSDAlertViewTools *)shareInstance;/***  创建提示框**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param titleArray   标题字符串数组(为nil,默认为"确定")*  @param vc           VC iOS8及其以后会用到*  @param confirm      点击按钮的回调(取消按钮的Index是cancelIndex -1)*/
- (void)showAlert:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitletitleArray:(NSArray *)titleArrayviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirm;/***  创建提示框(可变参数版)**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param vc           VC iOS8及其以后会用到*  @param confirm      点击按钮的回调(取消按钮的Index是cancelIndex -1)*  @param buttonTitles 按钮(为nil,默认为"确定",传参数时必须以nil结尾,否则会崩溃)*/
- (void)showAlert:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitleviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirmbuttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION;/***  创建菜单(Sheet)**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param titleArray   标题字符串数组(为nil,默认为"确定")*  @param vc           VC iOS8及其以后会用到*  @param confirm      点击确认按钮的回调(取消按钮的Index是cancelIndex -1)*/
- (void)showSheet:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitletitleArray:(NSArray *)titleArrayviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirm;/***  创建菜单(Sheet 可变参数版)**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param vc           VC*  @param confirm      点击按钮的回调(取消按钮的Index是cancelIndex -1)*  @param buttonTitles 按钮(为nil,默认为"确定",传参数时必须以nil结尾,否则会崩溃)*/
- (void)showSheet:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitleviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirmbuttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION ;@end

.m里
//
//  XSDAlertViewTools.m
//  XSDSH  提示框工具类
//
//  Created by 小广 on 16/1/11.
//  Copyright © 2016年 XSD. All rights reserved.
//#import "XSDAlertViewTools.h"#define RootVC  [[UIApplication sharedApplication] keyWindow].rootViewController@interface XSDAlertViewTools ()@property (nonatomic, copy) AlertViewBlock block;@end@implementation XSDAlertViewTools#pragma mark - 对外方法
+ (XSDAlertViewTools *)shareInstance {static XSDAlertViewTools *tools = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{tools = [[self alloc] init];});return tools;
}/***  创建提示框**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param titleArray   标题字符串数组(为nil,默认为"确定")*  @param vc           VC*  @param confirm      点击确认按钮的回调*/
- (void)showAlert:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitletitleArray:(NSArray *)titleArrayviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirm {//if (!vc) vc = RootVC;[self p_showAlertController:title message:messagecancelTitle:cancelTitle titleArray:titleArrayviewController:vc confirm:^(NSInteger buttonTag) {if (confirm)confirm(buttonTag);}];
}/***  创建提示框(可变参数版)**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param vc           VC*  @param confirm      点击按钮的回调*  @param buttonTitles 按钮(为nil,默认为"确定",传参数时必须以nil结尾,否则会崩溃)*/
- (void)showAlert:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitleviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirmbuttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION {// 读取可变参数里面的titles数组NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];va_list list;if(buttonTitles) {//1.取得第一个参数的值(即是buttonTitles)[titleArray addObject:buttonTitles];//2.从第2个参数开始,依此取得所有参数的值NSString *otherTitle;va_start(list, buttonTitles);while ((otherTitle = va_arg(list, NSString*))) {[titleArray addObject:otherTitle];}va_end(list);}if (!vc) vc = RootVC;[self p_showAlertController:title message:messagecancelTitle:cancelTitle titleArray:titleArrayviewController:vc confirm:^(NSInteger buttonTag) {if (confirm)confirm(buttonTag);}];}/***  创建菜单(Sheet)**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param titleArray   标题字符串数组(为nil,默认为"确定")*  @param vc           VC*  @param confirm      点击确认按钮的回调*/
- (void)showSheet:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitletitleArray:(NSArray *)titleArrayviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirm {if (!vc) vc = RootVC;[self p_showSheetAlertController:title message:message cancelTitle:cancelTitletitleArray:titleArray viewController:vc confirm:^(NSInteger buttonTag) {if (confirm)confirm(buttonTag);}];
}/***  创建菜单(Sheet 可变参数版)**  @param title        标题*  @param message      提示内容*  @param cancelTitle  取消按钮(无操作,为nil则只显示一个按钮)*  @param vc           VC iOS8及其以后会用到*  @param confirm      点击按钮的回调*  @param buttonTitles 按钮(为nil,默认为"确定",传参数时必须以nil结尾,否则会崩溃)*/
- (void)showSheet:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitleviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirmbuttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION {// 读取可变参数里面的titles数组NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];va_list list;if(buttonTitles) {//1.取得第一个参数的值(即是buttonTitles)[titleArray addObject:buttonTitles];//2.从第2个参数开始,依此取得所有参数的值NSString *otherTitle;va_start(list, buttonTitles);while ((otherTitle= va_arg(list, NSString*))) {[titleArray addObject:otherTitle];}va_end(list);}if (!vc) vc = RootVC;// 显示菜单提示框[self p_showSheetAlertController:title message:message cancelTitle:cancelTitletitleArray:titleArray viewController:vc confirm:^(NSInteger buttonTag) {if (confirm)confirm(buttonTag);}];}#pragma mark - ----------------内部方法------------------//UIAlertController(iOS8及其以后)
- (void)p_showAlertController:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitletitleArray:(NSArray *)titleArrayviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirm {UIAlertController  *alert = [UIAlertController alertControllerWithTitle:titlemessage:messagepreferredStyle:UIAlertControllerStyleAlert];// 下面两行代码 是修改 title颜色和字体的代码
//    NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName:UIColorFrom16RGB(0x334455)}];
//    [alert setValue:attributedMessage forKey:@"attributedTitle"];if (cancelTitle) {// 取消UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:cancelTitlestyle:UIAlertActionStyleCancelhandler:^(UIAlertAction * _Nonnull action) {if (confirm)confirm(cancelIndex);}];[alert addAction:cancelAction];}// 确定操作if (!titleArray || titleArray.count == 0) {UIAlertAction  *confirmAction = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {if (confirm)confirm(0);}];[alert addAction:confirmAction];} else {for (NSInteger i = 0; i<titleArray.count; i++) {UIAlertAction  *action = [UIAlertAction actionWithTitle:titleArray[i]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {if (confirm)confirm(i);}];// [action setValue:UIColorFrom16RGB(0x00AE08) forKey:@"titleTextColor"]; // 此代码 可以修改按钮颜色[alert addAction:action];}}[vc presentViewController:alert animated:YES completion:nil];}// ActionSheet的封装
- (void)p_showSheetAlertController:(NSString *)titlemessage:(NSString *)messagecancelTitle:(NSString *)cancelTitletitleArray:(NSArray *)titleArrayviewController:(UIViewController *)vcconfirm:(AlertViewBlock)confirm {UIAlertController *sheet = [UIAlertController alertControllerWithTitle:titlemessage:messagepreferredStyle:UIAlertControllerStyleActionSheet];if (!cancelTitle) cancelTitle = @"取消";// 取消UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:cancelTitlestyle:UIAlertActionStyleCancelhandler:^(UIAlertAction * _Nonnull action) {if (confirm)confirm(cancelIndex);}];[sheet addAction:cancelAction];if (titleArray.count > 0) {for (NSInteger i = 0; i<titleArray.count; i++) {UIAlertAction  *action = [UIAlertAction actionWithTitle:titleArray[i]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {if (confirm)confirm(i);}];[sheet addAction:action];}}[vc presentViewController:sheet animated:YES completion:nil];
}@end

用法:

 // 通常的alert[[XSDAlertViewTools shareInstance] showAlert:@"提示"message:@"这个就是提示的内容了"cancelTitle:@"取消"titleArray:@[@"确定"]viewController:nilconfirm:^(NSInteger buttonTag) {// cancel按钮的index(buttonTag)是-1 cancelIndexNSLog(@"点击按钮的buttonTag===%ld==",(long)buttonTag);}];
 // 带有可变参数的alert[[XSDAlertViewTools shareInstance] showAlert:@"提示"message:@"这个就是提示的内容了"cancelTitle:@"取消"viewController:selfconfirm:^(NSInteger buttonTag) {// cancel按钮的index(buttonTag)是-1 cancelIndexNSLog(@"点击按钮的buttonTag===%ld==",(long)buttonTag);} buttonTitles:@"呼叫",@"查看",@"我就看看", nil];

如图:alert 和sheet

  


比较low,大家凑合着看吧...其中的sheet的用法,和上面的alert一样;


这篇关于iOS开发 提示框UIAlertController的略微封装的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/775313

相关文章

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要