给UIAlertController添加一个分类

简介: 给UIAlertController添加一个分类

给UIAlertController添加一个分类


我们写代码时候,经常会用到弹框,然后到处都是写基本一样的代码,干脆给它做一个分类,封装起来,这样,直接调用方法。我这里仅仅提供了四种最常见的。使用方法非常简单,直接将UIViewController+Message 这个类拖入到项目中,导入头文件需要使用的地方,建议以后直接放pch

Dome: https://github.com/LYWGod/UIAlertControllerExtension

第一种:弹出UIAlertController  风格为 UIAlertControllerStyleAlert 且有取消和确定按钮

bb00b177bf27348d61f4a507517a59d3.png

Snip20161124_2.png

第二种:弹出UIAlertController 风格为UIAlertControllerStyleAlert 并且只有一个确定按钮

7b562fee589e401c3e358433560d70c1.png

Snip20161124_3.png

第三种:弹出UIAlertController 风格为UIAlertControllerStyleActionSheet并且有两个选择项和一个取消项

96643e60df54d71703bc2690c8201632.png

Snip20161124_4.png

第三种:弹出UIAlertController 风格为UIAlertControllerStyleActionSheet并且有一个选择项和一个取消项

bcc3abc0c13b410d1dabccc8638e53d9.png

Snip20161124_5.png

封装的方法非常简单。如果需要也可以直接下载

代码.h文件

#import <UIKit/UIKit.h>
@interface UIViewController (Message)
/**
 弹出UIAlertController
 @param title   标题
 @param message 消息
 @param sure    点击确定按钮
 */
- (void)showAlertSureWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure;
/**
 弹出UIAlerController
 @param title   标题
 @param message 消息
 @param sure    点击确定
 @param cancel  点击取消
 */
- (void)showAlertSureAndCancelWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure cancel:(void (^) (UIAlertAction *action))cancel;
/**
 弹出UIAlertController
 @param actionOneTitle 标题
 @param handlerOne     点击标题的事件
 */
- (void)showSheetOneaction:(NSString *)actionOneTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne;
/**
 弹出UIAlerController 
 @param actionOneTitle 第一标题
 @param actionTwoTitle 第二个标题
 @param handlerOne     第一个标题点击事件
 @param handlerTwo     第二个标题点击事件
 */
- (void)showSheetTwoaction:(NSString *)actionOneTitle actionTwo:(NSString *)actionTwoTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne handlerTwo:(void (^) (UIAlertAction *action))handlerTwo;
@end

.m文件

#import "UIViewController+Message.h"
@implementation UIViewController (Message)
/**
 弹出UIAlertController
 @param title   标题
 @param message 消息
 @param sure    点击确定按钮
 */
- (void)showAlertSureWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure;
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:sure];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}
/**
 弹出UIAlerController
 @param title   标题
 @param message 消息
 @param sure    点击确定
 @param cancel  点击取消
 */
- (void)showAlertSureAndCancelWithTitle:(NSString *)title message:(NSString *)message sure:(void (^) (UIAlertAction *action))sure cancel:(void (^) (UIAlertAction *action))cancel
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:sure];
    UIAlertAction *revoke = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancel];
    [alert addAction:action];
    [alert addAction:revoke];
    [self presentViewController:alert animated:YES completion:nil];
}
/**
 弹出UIAlertController
 @param actionOneTitle 标题
 @param handlerOne     点击标题的事件
 */
- (void)showSheetOneaction:(NSString *)actionOneTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne
{
    UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:actionOneTitle style:UIAlertActionStyleDefault handler:handlerOne];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertSheet addAction:actionOne];
    [alertSheet addAction:cancelAction];
    [self presentViewController:alertSheet animated:YES completion:nil];
}
/**
 弹出UIAlerController
 @param actionOneTitle 第一标题
 @param actionTwoTitle 第二个标题
 @param handlerOne     第一个标题点击事件
 @param handlerTwo     第二个标题点击事件
 */
- (void)showSheetTwoaction:(NSString *)actionOneTitle actionTwo:(NSString *)actionTwoTitle handlerOne:(void(^)(UIAlertAction *action))handlerOne handlerTwo:(void (^) (UIAlertAction *action))handlerTwo
{
    UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:actionOneTitle style:UIAlertActionStyleDefault handler:handlerOne];
    UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:actionTwoTitle style:UIAlertActionStyleDefault handler:handlerTwo];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertSheet addAction:actionOne];
    [alertSheet addAction:actionTwo];
    [alertSheet addAction:cancelAction];
    [self presentViewController:alertSheet animated:YES completion:nil];
}
@end


目录
打赏
0
0
0
0
1
分享
相关文章
kde
|
20天前
|
Docker镜像加速指南:手把手教你配置国内镜像源
配置国内镜像源可大幅提升 Docker 拉取速度,解决访问 Docker Hub 缓慢问题。本文详解 Linux、Docker Desktop 配置方法,并提供测速对比与常见问题解答,附最新可用镜像源列表,助力高效开发部署。
kde
10702 82
连续四年,阿里云再获市场份额第一
IDC报告:阿里云再获公有云云工作负载安全市场份额第一!
阿里云百炼全新发布Qwen3-Coder-Plu并开源,重磅升级Qwen3-235B
面向全球的智能编程引擎Qwen3-Coder正式开源!首发旗舰版Qwen3-Coder-480B-A35B-Instruct,拥有480B参数、35B有效参数,基于MoE架构,模型在Agentic Coding、浏览器智能操作及基础编码任务中刷新SOTA纪录,并同步开源完整工具链,包括Qwen Code命令行工具、Claude Code集成、VS Code插件及阿里云平台API支持。Qwen3-Coder支持256K上下文,最高可扩展至1M,适用于仓库级代码理解。通过强化学习技术,实现多轮交互与自主决策,大幅提升代码执行成功率。开发者可通过阿里云百炼平台直接体验或调用API使用。
779 0
云上十五年——弹性计算系列客户故事(一)
技术的价值在于解决真实业务挑战。为记录这十五年的共同成长,阿里云特别推出《云上十五年》客户故事专栏,分享与各行业伙伴的实践成果。新起点开启新征程,阿里云持续突破核心技术,让云计算的“澎湃算力”,持续为各行各业提供“创新动力”。
云上十五年——弹性计算系列客户故事(一)
Windows安装Claude Code
Claude Code 是 Anthropic 推出的代码助手,支持在 Windows 通过 WSL(Windows Subsystem for Linux)运行。本文介绍如何在 Windows 系统中启用 WSL、安装 Ubuntu 子系统、配置 Python 与 Node.js 环境,并最终安装和运行 Claude Code。内容涵盖 WSL 设置、开发工具安装、依赖配置及常见问题解决方法,助你顺利在本地环境中使用 Claude Code 提升编码效率。
758 2
Windows安装Claude Code
抖音集团基于Flink的亿级RPS实时计算优化实践
本文整理自抖音集团在 Flink Forward Asia 2024 的分享,围绕实时计算链路优化,涵盖现状与挑战、通用优化策略、视频与直播场景专项优化及未来规划,旨在提升稳定性、降低资源消耗并支持复杂业务需求。
309 0
抖音集团基于Flink的亿级RPS实时计算优化实践
让复杂 AI 应用构建就像搭积木:Spring AI Alibaba Graph 使用指南与源码解读
通过指南和完整的示例项目,你可以快速掌握 Spring AI Alibaba Graph 的使用方法,并在实际项目中高效地构建智能化应用。
617 22
淘天AB实验分析平台Fluss落地实践:更适合实时OLAP的消息队列
淘天集团数据开发团队基于Fluss构建新一代实时数仓,解决数据消费冗余、探查困难及大State运维难题。Fluss融合列存与实时更新能力,支持列裁剪、KV点查、Delta Join及湖流一体,显著降低IO与计算资源消耗,提升作业稳定性与数据探查效率。已在淘天AB实验平台落地,覆盖搜索、推荐等核心业务,通过618大促验证,实现千万级流量、秒级延迟,资源消耗降低30%,State缩减超100TB。未来将持续深化湖仓架构,拓展AI场景应用。
420 4
淘天AB实验分析平台Fluss落地实践:更适合实时OLAP的消息队列
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等