博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios实战-使用Block的UIAlertView
阅读量:6230 次
发布时间:2019-06-21

本文共 7297 字,大约阅读时间需要 24 分钟。

hot3.png

因为block比较好用,你懂得,直接上代码:

.h

typedef void (^choiceCompletionBlock)(int index);
+ (instancetype)shareAlertView;#pragma mark - 弹窗//普通提示弹窗- (void)showTipAlert:(NSString *)message;- (void)showTipAlert:(NSString *)message completion:(choiceCompletionBlock)completion;//可以选择的弹窗- (void)showChoiceAlert:(NSString *)message completion:(choiceCompletionBlock)completion;- (void)showChoiceAlert:(NSString *)message doneTitle:(NSString *)doneTitile completion:(choiceCompletionBlock)completion;- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 completion:(choiceCompletionBlock)completion;// 可自定义Title- (void)showChoiceAlert:(NSString *)message title:(NSString *)title doneTitle:(NSString *)doneTitle completion:(choiceCompletionBlock)completion;- (void)showChoiceAlert:(NSString *)message title:(NSString *)title button1Title:(NSString *)button1Title button2Title:(NSString *)button2Title completion:(choiceCompletionBlock)completion;//三选择弹窗- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 button3Title:(NSString *)title3 completion:(choiceCompletionBlock)completion;

.m  

choiceCompletionBlock _choiceCompletion;
- (void)showTipAlert:(NSString *)message{    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];    [alert show];}- (void)showTipAlert:(NSString *)message completion:(choiceCompletionBlock)completion{    if (_choiceCompletion) {        return;    }        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];    [alert show];        _choiceCompletion = [completion copy];}- (void)showChoiceAlert:(NSString *)message completion:(choiceCompletionBlock)completion{    [self showChoiceAlert:message doneTitle:@"确定" completion:completion];}- (void)showChoiceAlert:(NSString *)message doneTitle:(NSString *)doneTitile completion:(choiceCompletionBlock)completion{    [self showChoiceAlert:message title:TIPTITLE doneTitle:doneTitile completion:completion];}- (void)showChoiceAlert:(NSString *)message title:(NSString *)title doneTitle:(NSString *)doneTitle completion:(choiceCompletionBlock)completion {    if (_choiceCompletion) {        return;    }        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:doneTitle, nil];    [alert show];    _choiceCompletion = [completion copy];}- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 completion:(choiceCompletionBlock)completion{    if (_choiceCompletion) {        return;    }        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, nil];    [alert show];    _choiceCompletion = [completion copy];}- (void)showChoiceAlert:(NSString *)message title:(NSString *)title button1Title:(NSString *)button1Title button2Title:(NSString *)button2Title completion:(choiceCompletionBlock)completion {    if (_choiceCompletion) {        return;    }        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:button1Title, button2Title, nil];    [alert show];    _choiceCompletion = [completion copy];}- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 button3Title:(NSString *)title3 completion:(choiceCompletionBlock)completion{    if (_choiceCompletion) {        return;    }        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, title3, nil];    [alert show];    _choiceCompletion = [completion copy];}
#pragma mark - UIAlertViewDelegate- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (_choiceCompletion) {        _choiceCompletion(buttonIndex);        _choiceCompletion = nil;    }}

上面的代码会有个问题,就是假如代码多次调用alert的时候就只能显示一个了,解决的办法我想到了一个不错的。

#import 
#define EOCMyAlertViewKey @"EOCMyAlertViewKey"
- (void)showTipAlert:(NSString *)message{    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];    [alert show];}- (void)showTipAlert:(NSString *)message completion:(choiceCompletionBlock)completion{    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];    objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);    [alert show];    }- (void)showChoiceAlert:(NSString *)message completion:(choiceCompletionBlock)completion{    [self showChoiceAlert:message doneTitle:@"确定" completion:completion];}- (void)showChoiceAlert:(NSString *)message doneTitle:(NSString *)doneTitile completion:(choiceCompletionBlock)completion{    [self showChoiceAlert:message title:TIPTITLE doneTitle:doneTitile completion:completion];}- (void)showChoiceAlert:(NSString *)message title:(NSString *)title doneTitle:(NSString *)doneTitle completion:(choiceCompletionBlock)completion {    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:doneTitle, nil];    objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);    [alert show];}- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 completion:(choiceCompletionBlock)completion{    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, nil];    objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);    [alert show];}- (void)showChoiceAlert:(NSString *)message title:(NSString *)title button1Title:(NSString *)button1Title button2Title:(NSString *)button2Title completion:(choiceCompletionBlock)completion {    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:button1Title, button2Title, nil];    objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);    [alert show];}- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 button3Title:(NSString *)title3 completion:(choiceCompletionBlock)completion{    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, title3, nil];    objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);    [alert show];}
#pragma mark - UIAlertViewDelegate- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    choiceCompletionBlock block = objc_getAssociatedObject(alertView, EOCMyAlertViewKey);    if (block) {        block(buttonIndex);    }}

使用runtime中的关联就能很好的解决问题,也不用数组什么的!

转载于:https://my.oschina.net/iq19900204/blog/402933

你可能感兴趣的文章
南京大学周志华教授当选欧洲科学院外籍院士
查看>>
《OpenGL ES应用开发实践指南:Android卷》—— 1.3 初始化OpenGL
查看>>
PHP 数值
查看>>
Javascript 中的上下文
查看>>
使用scrapy的定制爬虫-第二章-概
查看>>
枚举类型 enum,NS_ENUM,NS_OPTIONS
查看>>
Ez×××客户端在服务器侧没有配置隧道分离的情况下如何直接上公网
查看>>
list集合练习笔记
查看>>
SQLserver From simple To Full backup model
查看>>
Centos7.5-文件权限管理
查看>>
tomcat虚拟主机 server.xml文件配置
查看>>
Capture Nx
查看>>
OC中的NSSet(集合)
查看>>
马士兵教学语录
查看>>
计算机网络与Internet应用
查看>>
每天一个linux命令-mkdir
查看>>
四天精通shell编程(二)
查看>>
标签制作软件中如何导出标签模板为PDF文件?
查看>>
Linux运维系统工程师系列---22
查看>>
我的友情链接
查看>>