前言:

上篇文章写的是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的更多相关文章

  1. Objective C Runtime 开发介绍

    简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...

  2. iOS 开发--github的demo

    令人惊讶的是,YYText 虽然代码量很大(超过一万行),但它只是 ibireme 的作品之一.ibireme 利用业余时间完成了 YYKit 工具库,包括: YYModel — 高性能的 iOS J ...

  3. 刨根问底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 ...

  4. Objective-C Runtime(一)预备知识

    很早就知道了Objective-C Runtime这个概念,「Objective-C奇技淫巧」「iOS黑魔法」各种看起来很屌的主题中总会有它的身影:但一直没有深入去学习,一来觉得目前在实际项目中还没有 ...

  5. iOS-运行时机制

    这里的两篇运行时的文章感觉还不错. 收藏: 初识iOS运行时RunTime | // TODO: http://www.saitjr.com/ios/objc-runtime.html Objecti ...

  6. 据说是百度ios面试题

    百度面试题:   一面:知识点 Objective C runtime library: Objective C的对象模型,Block的底层实现结构,消息发送,消息转发,内存管理 CoreData : ...

  7. iOS 面试基础题目

    转载: iOS 面试基础题目 题目来自博客:面试百度的记录,有些问题我能回答一下,不能回答的或有更好的回答我放个相关链接供参考. 1面 Objective C runtime library:Obje ...

  8. objc_msgSend消息传递学习笔记 – 消息转发

    该文是 objc_msgSend消息传递学习笔记 – 对象方法消息传递流程 的基础上继续探究源码,请先阅读上文. 消息转发机制(message forwarding) Objective-C 在调用对 ...

  9. ios Object Encoding and Decoding with NSSecureCoding Protocol

    Object Encoding and Decoding with NSSecureCoding Protocol February 27, 2014 MISC NSCoding is a fanta ...

随机推荐

  1. java中接口与多重继承的关系

    在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和int ...

  2. Vijos1404 遭遇战 最短路,dijkstra,堆

    P1404遭遇战 标签:[显示标签]     背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分 ...

  3. 8.1.C++ AMP简介

    C++ AMP是专为设计支持C++的异构并行模型. 全程是: Accelerator Massive Parallelism 下面是一个Vector C++ AMP的代码,通过这段代码来解释C++ A ...

  4. 解决adb问题的方法

    The connection to adb is down,and a server error has occured. 在网上找的那个高端方法根本不管用,来,试试我的方法.. 先装个360手机助手 ...

  5. 恒天云技术分享系列4 – OpenStack网络攻击与防御

    恒天云技术分享系列:http://www.hengtianyun.com/download-show-id-13.html 云主机的网络结构本质上和传统的网络结构一致,区别大概有两点. 1.软网络管理 ...

  6. 如何写出性能好的sql

    开发人员是很少注意SQL对数据库性能影响的重要性的,大多程序员都会认为SQL是比较简单的,需要的时候查查手册就可以了,很少有深究的. 这样的观念对大型系统的开发是致命的,需要纠正这样的观念. 造成这样 ...

  7. HDU 3488--Tour(KM or 费用流)

    因为每个点只能经过一次 所以考虑拆点 这题有坑,有重边.. KM算法 把一个点拆成入点和出点 入点在X部,出点在Y步. 如果u,v之间有路径,就在X部的u点连接Y部的v点 求完美匹配. 当完美匹配的时 ...

  8. POJ2778&HDU2243&POJ1625(AC自动机+矩阵/DP)

    POJ2778 题意:只有四种字符的字符串(A, C, T and G),有M中字符串不能出现,为长度为n的字符串可以有多少种. 题解:在字符串上有L中状态,所以就有L*A(字符个数)中状态转移.这里 ...

  9. mssql存储过程demo

    ALTER PROCEDURE [dbo].[sp_get_saleData]ASBEGIN set nocount on -- 获取最近上传数据的时间戳 declare @dd datetime s ...

  10. nginx编译参数集合

    http://www.ttlsa.com/nginx/nginx-configure-descriptions/ 标题是不是很欠揍,个人认为确实值得一看,如果你不了解nginx,或者你刚学nginx, ...