给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


相关文章
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
7 Papers & Radios | OpenAI文本生成图像新模型GLIDE;培养皿中百万人脑细胞打乒乓(2)
7 Papers & Radios | OpenAI文本生成图像新模型GLIDE;培养皿中百万人脑细胞打乒乓
|
11月前
|
机器学习/深度学习 自然语言处理 并行计算
7 Papers & Radios | OpenAI文本生成图像新模型GLIDE;培养皿中百万人脑细胞打乒乓(1)
7 Papers & Radios | OpenAI文本生成图像新模型GLIDE;培养皿中百万人脑细胞打乒乓
|
Android开发
RatingBar(星级评分条)
RatingBar(星级评分条)也非常简单,相信在某宝, 买过东西的对这个应该不陌生,收到卖家的包裹,里面很多时候会有个小纸片,五星好评返还多少元这样, 而评分的时候就可以用到我们这个星级评分条了。
97 0
UIAlertController简单使用
UIAlertController简单使用
84 0
UIAlertController简单使用
|
iOS开发
UIView的分类
UIView的分类
60 0
UIView的分类
给UILabel控件添加一个分类
给UILabel控件添加一个分类
68 0
给UILabel控件添加一个分类
|
开发者
分类控件| 学习笔记
快速学习分类控件。
51 0
|
开发者
分类控件|学习笔记
快速学习分类控件
71 0
分类控件|学习笔记
如何更好的限制一个UITextField/UITextView的输入字数
要限制一个UITextField/UITextView的输入字数,首先想到的应该是通过UITextFieldDelegate/UITextViewDelegate 的代理方法来限制,那么如何来更好的限制输入字数呢,下面我们来看看:
|
JSON 数据格式
一个实用的 NSString 分类工具
一个实用的NSString分类工具 (一)—— 玩转数字一个实用的NSString分类工具(二) —— 获取拼音大写首字母和转义等一个实用的NSString分类工具(三) —— json与OC对象转换及加解密
857 0