Objective-C Runtime之着魔的UIAlertView
前言:
上篇文章写的是Runtime的一个入门教程,刚哥问我那个Associated Objects加回调是啥时候用,那我就来告诉你啦!我们在使用UIAlertView的时候用的多。
传统的UIAlertView:
在一个类中有多个UIAlertView,不同的UIAlertView对应不同的事件,我们使用的传统方法如下:
#pragma mark - action method - (IBAction)firstButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.tag = ;
[alertView show];
} - (IBAction)secondButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.tag = ;
[alertView show];
} - (IBAction)ThirdButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.tag = ;
[alertView show];
} #pragma mark - delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1001执行ok");
}
} else if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1002执行ok");
}
} else if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1003执行ok");
}
}
}
我们要给每个UIAlertView赋值一个tag值,在delegate方法中还要进行tag的判断以及buttonIndex的判断,太繁琐了。
着魔的UIAlertView:
下面我们使用Category和Associated Objects进行魔法修改
创建一个UIAlertView的Category
UIAlertView+ActionBlock.h
#import <UIKit/UIKit.h> typedef void (^AlertCallBack)(UIAlertView *, NSUInteger); @interface UIAlertView (ActionBlock)<UIAlertViewDelegate> @property (nonatomic, copy) AlertCallBack callBack; @end
UIAlertView+ActionBlock.m
#if TARGET_IPHONE_SIMULATOR
#import <objc/objc-runtime.h>
#else
#import <objc/runtime.h>
#import <objc/message.h>
#endif @implementation UIAlertView (ActionBlock) - (void)setCallBack:(AlertCallBack)callBack
{
objc_setAssociatedObject(self, @selector(callBack), callBack, OBJC_ASSOCIATION_COPY_NONATOMIC);
self.delegate = self;
} - (AlertCallBack)callBack
{
return objc_getAssociatedObject(self, @selector(callBack));
} #pragma mark - delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (self.callBack) {
self.callBack(alertView, buttonIndex);
}
}
在主类中取消delegate,使用block属性
#pragma mark - action method - (IBAction)firstButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
if (buttonIndex == ) {
NSLog(@"魔法alertView1001执行ok");
}
};
[alertView show];
} - (IBAction)secondButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
if (buttonIndex == ) {
NSLog(@"魔法alertView1002执行ok");
}
};
[alertView show];
} - (IBAction)ThirdButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
if (buttonIndex == ) {
NSLog(@"魔法alertView1003执行ok");
}
};
[alertView show];
}
我们通过使用Category给UIAlertView扩展了一个block属性,当block被设置后就会调用setCallBack方法,触发self.delegate = self,即主类中的UIAlertView的delegate方法被Category中的方法覆盖。这样不仅有效解决问题,还解决了其他人修改该类的安全性(block被去掉后,原delegate恢复)
如下不给tag值为1003的UIAlertView设置block,即调用原delegate方法。
- (IBAction)firstButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
if (buttonIndex == ) {
NSLog(@"魔法alertView1001执行ok");
}
};
alertView.tag = ;
[alertView show];
} - (IBAction)secondButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
if (buttonIndex == ) {
NSLog(@"魔法alertView1002执行ok");
}
};
alertView.tag = ;
[alertView show];
} - (IBAction)ThirdButtonClick:(id)sender {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertView.tag = ;
[alertView show];
} - (IBAction)fourthButtonClick:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *alertAction){
NSLog(@"如果你是iOS8以上的应用,这个适合你,简单明了");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
} #pragma mark - delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1001执行ok");
}
} else if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1002执行ok");
}
} else if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1003执行ok");
}
} else if (alertView.tag == ) {
if (buttonIndex == ) {
NSLog(@"普通alertView1004执行ok");
}
}
}
相关Demo下载:
https://github.com/ianisme/UIAlertViewBYRuntime_Demo
总结:
通过Associated Objects我们有效的解决了UIAlertView的繁琐问题,如果您是开发iOS8以上的应用,建议您弃用UIAlertView,苹果的UIAlertController已经有了更好的解决方案。
Objective-C Runtime之着魔的UIAlertView的更多相关文章
- Objective C Runtime 开发介绍
简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...
- iOS 开发--github的demo
令人惊讶的是,YYText 虽然代码量很大(超过一万行),但它只是 ibireme 的作品之一.ibireme 利用业余时间完成了 YYKit 工具库,包括: YYModel — 高性能的 iOS J ...
- 刨根问底Objective-C Runtime(4)- 成员变量与属性
http://chun.tips/blog/2014/11/08/bao-gen-wen-di-objective[nil]c-runtime(4)[nil]-cheng-yuan-bian-lian ...
- Objective-C Runtime(一)预备知识
很早就知道了Objective-C Runtime这个概念,「Objective-C奇技淫巧」「iOS黑魔法」各种看起来很屌的主题中总会有它的身影:但一直没有深入去学习,一来觉得目前在实际项目中还没有 ...
- iOS-运行时机制
这里的两篇运行时的文章感觉还不错. 收藏: 初识iOS运行时RunTime | // TODO: http://www.saitjr.com/ios/objc-runtime.html Objecti ...
- 据说是百度ios面试题
百度面试题: 一面:知识点 Objective C runtime library: Objective C的对象模型,Block的底层实现结构,消息发送,消息转发,内存管理 CoreData : ...
- iOS 面试基础题目
转载: iOS 面试基础题目 题目来自博客:面试百度的记录,有些问题我能回答一下,不能回答的或有更好的回答我放个相关链接供参考. 1面 Objective C runtime library:Obje ...
- objc_msgSend消息传递学习笔记 – 消息转发
该文是 objc_msgSend消息传递学习笔记 – 对象方法消息传递流程 的基础上继续探究源码,请先阅读上文. 消息转发机制(message forwarding) Objective-C 在调用对 ...
- ios Object Encoding and Decoding with NSSecureCoding Protocol
Object Encoding and Decoding with NSSecureCoding Protocol February 27, 2014 MISC NSCoding is a fanta ...
随机推荐
- js变量作用域
for(var i =0;i<100;i++) { } alert(i);//100 if(true){ var i="91d"; } alert(i);//91d func ...
- JPA--联合主键
联合主键的一些知识: 使用@EmbeddedId标示联合主键: 在联合主键类中只是定义确定联合主键的字段即可: * 联合主键类的规则 * 1.必须包含一个无参的构造函数 * 2.必须实现序列化接口 * ...
- SharePoint 2010 自定义 字段 类型--------省市区联动
转:http://www.cnblogs.com/sp007/p/3384310.html 最近有几个朋友问到了有关自定义字段类型的问题,为了让更多的人了解自定义字段类型的方法,特写一篇博客与大家分享 ...
- 《深入Java虚拟机学习笔记》- 第14章 浮点运算
<深入Java虚拟机学习笔记>- 第13章 浮点运算
- delphi 数据导出到word
procedure TFrmWeekAnalysisQry.BtnExportToExcelClick(Sender: TObject);var wordApp,WordDoc,WrdSelectio ...
- 建立第一个OpenGL工程(GLUT)
本文参考了<计算机图形学>(Donald Hearn著)的第2.9节. OpenGL基本函数库用来描述图元.属性.几何变换.观察变换和进行许多其他的操作.OpenGL被设计成与硬件无关,因 ...
- [Orchard CMS系列] 创建主题(Writing a new theme)
本文需要对Orchard CMS有基本了解. 开启模块 code generation 创建新的主题工程骨架 Codegen theme MyTheme 创建主题样式 src\Orchard.Web\ ...
- dataStructure@ Find if there is a path between two vertices in a directed graph
Given a Directed Graph and two vertices in it, check whether there is a path from the first given ve ...
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- [iOS基础控件 - 6.3] 使用可视化连线方式指定dataSource、delegate
对着要指定dataSource或者delegate的控件右击,然后拖动线到指定的控制器上