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 ...
随机推荐
- session问题
如果 <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424&q ...
- WCF HTTPS配置
昨天需要把做好的一个wcf服务发布到服务器站点下的一个虚拟目录中发布过程遇到了一个问题:服务器上的环境是https,因此需要多对配置文件修改于是在网上找啊找,遇到一个问题找一个问题,可是问题依然没解决 ...
- JS分页 + 获取MVC地址栏URL路径的最后参数
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- WPF学习笔记 - 在XAML里绑定
Binding除了默认构造函数外,还有一个可以传入Path的构造函数,下面两种方式实现的功能是一样的. <TextBlock x:Name="currentFolder" D ...
- 设计模式_Interpreter_解释器模式
形象例子: 俺有一个<泡MM真经>,上面有各种泡MM的攻略,比如说去吃西餐的步骤.去看电影的方法等等,跟MM约会时,只要做一个Interpreter,照着上面的脚本执行就可以了.解释器模式 ...
- java 压缩技术
package zip; import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStr ...
- 简易博客编辑器:玩转document.execCommand命令
xhEditor是基于jQuery开发的跨平台轻量可视化XHTML编辑器,基于LGPL开源协议发布.今天就把它山寨一下. 上几张图,看看效果: 先做出菜单部分:发现是一张背景图片,所以用图片映射的方法 ...
- 初识Ajax---简单的Ajax应用实例
原文: http://www.ido321.com/347.html 从网页前端输入提示范围内的字符,然后显示从后台返回的结果 1: <html> 2: <head> 3: & ...
- codeforces 617B Chocolate
题意: 在给定01串中,问能分割成多少个子串?每个子串只有一个1. dp #include<iostream> #include<string> #include<alg ...
- leetcode–Binary Tree Maximum Path Sum
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...