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 ...
随机推荐
- ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例
在实际的ASP.NET mvc项目开发中,有时会遇到一个参数是一个List.Dictionary等集合类型的情况,默认的情况ASP.NET MVC框架是怎么为我们绑定ASP.NET MVC的Actio ...
- HDU 1405 The Last Practice
The Last Practice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- mina2
远程通信 Mina2 学习笔记 作者:李少华 邮箱:xiaosanshaoli@126.com QQ:305409913 2010-12-23 初稿 引言... 1 一. Mina ...
- 7、四大组件之二-Service高级
一.Native Service 1>什么是Native Service 使用JNI编写,在系统启动完成之前启动的系统级服务. 2>哪些服务是Native Service ACCESSIB ...
- HDU 5634 Rikka with Phi 线段树
题意:bc round 73 div1 D 中文题面 分析:注意到10^7之内的数最多phi O(log(n))次就会变成1, 因此可以考虑把一段相同的不为1的数缩成一个点,用平衡树来维护. 每次求p ...
- linux删除数据文件无备份恢复
参考 : http://www.lunar2013.com/2013/06/linux-%E8%AF%AF%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6%E6%81%A2%E ...
- 020自动化测试 PK 手动测试
一.手工测试为什么不可替代 手工测试是不可替代的,因为人是具有很强只能判断能力的,而工具是相对机械缺乏思维能力的东西 工具是人开发出来的 二.手工测试不可替代的表现 测试用例的设计:需要tester有 ...
- CentOS 7 nfs客户端挂载问题
配置nfs服务器时,服务器端挂载没有问题,但是客户端挂载时提示:mount.nfs: Connection timed out 解决方法1: 关闭firewalld # systemctl stop ...
- jquery 日期控件
控件官网: http://www.interidea.org/demo/icalendar.php#demohtml绑定控件 $("#startdate").icalendar({ ...
- HW6.5
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...