给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


相关文章
|
NoSQL MongoDB
mongoTemplate批量保存数据mongoDB批量保存数据
mongoTemplate批量保存数据mongoDB批量保存数据
724 2
|
存储 C语言
用加法器实现补码的加/减运算
用加法器实现补码的加/减运算
1084 0
|
开发框架 IDE Java
java制作游戏,如何使用libgdx,入门级别教学
本文是一篇入门级教程,介绍了如何使用libgdx游戏开发框架创建一个简单的游戏项目,包括访问libgdx官网、设置项目、下载项目生成工具,并在IDE中运行生成的项目。
949 1
java制作游戏,如何使用libgdx,入门级别教学
网络拓扑有哪些类型?
【8月更文挑战第19天】网络拓扑有哪些类型?
908 1
|
数据采集 监控 安全
网络爬虫是什么,它有什么作用?
网络爬虫是自动化工具,用于从网站中提取信息,通过追踪超链接和分析网页内容,实现互联网数据的自动搜集与整理。其工作流程包括选择起始URL、下载网页、解析HTML、跟踪链接、提取和存储数据及定期更新。主要用途涵盖数据挖掘、内容聚合、搜索引擎索引、价格比较、网站监控、学术研究及安全合规性等方面。然而,使用时需注意隐私、版权等法律问题。使用动态IP可避免触发网站反爬机制,如选用优质海外代理IP服务提高效率。
|
数据采集 机器学习/深度学习 搜索推荐
Python爬虫技术基础与应用场景详解
本文介绍了爬虫技术的基本概念、原理及应用场景,包括数据收集、价格监测、竞品分析和搜索引擎优化等。通过一个实战案例展示了如何使用Python爬取电商网站的商品信息。强调了在使用爬虫技术时需遵守法律法规和道德规范,确保数据抓取的合法性和合规性。
|
存储 算法 数据格式
一篇文章讲明白Mipmap与纹理过滤
一篇文章讲明白Mipmap与纹理过滤
679 1
|
关系型数据库 MySQL 机器人
【MySQL】两个脚本自动化搞定 MySQL 备份恢复--XtraBackup
【MySQL】两个脚本自动化搞定 MySQL 备份恢复--XtraBackup
|
机器学习/深度学习 监控 数据可视化
Scikit-learn与可视化:让机器学习结果更直观
【4月更文挑战第17天】本文探讨了如何使用Scikit-learn和可视化工具使机器学习结果更直观。Scikit-learn作为Python的开源机器学习库,结合Matplotlib、Seaborn等可视化库,便于数据探索、模型训练过程监控及结果展示。通过示例代码,展示了数据探索的pairplot、模型训练准确率曲线的绘制以及聚类结果的散点图,强调了可视化在提升模型理解度和应用普及性上的作用。随着可视化技术进步,机器学习将变得更直观易懂。
|
Scala
Scala语言入门二(对象)
讲述Scala中的面向对象相关知识点
1378 148