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

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

v0.dev快速开发

探索v0.dev:次世代开发者之利器 今之技艺日新月异,开发者之工具亦随之进步不辍。v0.dev者,新兴之开发者利器也,迅速引起众多开发者之瞩目。本文将引汝探究v0.dev之基本功能与优势,助汝速速上手,提升开发之效率。 何谓v0.dev? v0.dev者,现代化之开发者工具也,旨在简化并加速软件开发之过程。其集多种功能于一体,助开发者高效编写、测试及部署代码。无论汝为前端开发者、后端开发者