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

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties