• runtime运行时用法之一 --- 交换类的方法,此处简单写了把系统的UIView的setBackgroundColor的方法换成了自定义的pb_setBackgroundColor
  • 首先创建UIView的分类
  • 在分类中导入头文件#import <objc/runtime.h>
  • 实现load类方法 --- 类被加载运行的时候就会调用
  • 分别获取系统setBackgroundColor方法 和自定义的 pb_setBackgroundColor 方法.然后交换
  • 在AFNetworking中也有应用,AFN中利用runtime将访问网络的方法做了替换,替换后可以监听网络连接状态
static inline void af_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);

#import "UIView+BlackView.h" /** 导入头文件 */
#import <objc/runtime.h> @implementation UIView (BlackView) +(void)load{ /** 获取原始setBackgroundColor方法 */
Method originalM = class_getInstanceMethod([self class], @selector(setBackgroundColor:)); /** 获取自定义的pb_setBackgroundColor方法 */
Method exchangeM = class_getInstanceMethod([self class], @selector(pb_setBackgroundColor:)); /** 交换方法 */
method_exchangeImplementations(originalM, exchangeM);
} /** 自定义的方法 */
-(void)pb_setBackgroundColor:(UIColor *) color{ NSLog(@"%s",__FUNCTION__); /**
1.更改颜色
2.所有继承自UIView的控件,设置背景色都会设置成自定义的'orangeColor'
3. 此时调用的方法 'pb_setBackgroundColor' 相当于调用系统的 'setBackgroundColor' 方法,原因是在load方法中进行了方法交换.
4. 注意:此处并没有递归操作.
*/
[self pb_setBackgroundColor:[UIColor orangeColor]];
} @end
  • 控制器中测试
  • 分别创建一个Button 以及一个 View 并且设置好颜色,看效果

- (void)viewDidLoad {
[super viewDidLoad]; UIButton * btn = [UIButton new];
btn.backgroundColor = [UIColor blackColor];
[self.view addSubview:btn];
btn.frame = CGRectMake(0, 30, 200, 40);
[btn setTitle:@"点击" forState:UIControlStateNormal]; UIView * viw = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
viw.backgroundColor = [UIColor blueColor];
[self.view addSubview:viw]; }
  • 效果如下:

ios - runtime运行时应用---交换方法的更多相关文章

  1. iOS runtime 运行时( 二 )

    我们在编程过程中,如果使用到了runtime(运行时),我们几乎都是想动态的改变这个类的信息,包括方法,属性,balabala的,并且获得这个类的一些信息,等等,下面我们就来看看怎么通过runtime ...

  2. Objective-C Runtime 运行时之三:方法与消息

    基础数据类型 SEL SEL又叫选择器,是表示一个方法的selector的指针,其定义如下: typedef struct objc_selector *SEL; objc_selector结构体的详 ...

  3. Objective-C Runtime 运行时之三:方法与消息(转载)

    前面我们讨论了Runtime中对类和对象的处理,及对成员变量与属性的处理.这一章,我们就要开始讨论Runtime中最有意思的一部分:消息处理机制.我们将详细讨论消息的发送及消息的转发.不过在讨论消息之 ...

  4. iOS runtime 运行时( - )

    谈到运行时,相对应的就有编译时: 1).运行时-- 直到程序运行时才去确定一个对象的具体信息,并且可以改变这个类的具体信息,包括它的方法,变量等等: 2).编译时-- 是在程序运行之前,编译的时候,就 ...

  5. iOS runtime 运行时( 三 )

    上一篇 聊的是: 在不知道一个对象有什么属性的情况下, 获取对象的所有属性,然后可以改变属性的值, 今天我们谈谈 怎么通过Category(类别)+runtime 给一个类 添加一个新的属性,一般情况 ...

  6. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

  7. Runtime运行时的那点事儿

    注:本文是对 Colin Wheeler 的 Understanding the Objective-C Runtime 的翻译. 初学 Objective-C(以下简称ObjC) 的人很容易忽略一个 ...

  8. runtime 运行时机制 完全解读

    runtime 运行时机制 完全解读   目录[-] import import 我们前面已经讲过一篇runtime 原理,现在这篇文章主要介绍的是runtime是什么以及怎么用!希望对读者有所帮助! ...

  9. 编译器设计-RunTime运行时环境

    编译器设计-RunTime运行时环境 Compiler Design - Run-Time Environment 作为源代码的程序仅仅是文本(代码.语句等)的集合,要使其活动,它需要在目标计算机上执 ...

随机推荐

  1. hdu 2489(枚举 + 最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 思路:由于N, M的范围比较少,直接枚举所有的可能情况,然后求MST判断即可. #include ...

  2. 浅谈SQLiteOpenHelper之onUpgrade例子

    当你看到这个博文,首先你要了解onCreate这个创建方法,再来继续下文!(可以参考我的上一个博文http://www.cnblogs.com/896240130Master/p/6119616.ht ...

  3. 《DSP using MATLAB》示例Example5.7

    代码: x = [1, 1, 1, 1, zeros(1,4)]; N = 8; % zero-padding operation X_DFT = dft(x,N); % DFT of x(n) ma ...

  4. 【BZOJ4008】[HNOI2015]亚瑟王 期望

    [BZOJ4008][HNOI2015]亚瑟王 Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最 ...

  5. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  6. ZeroMQ接口函数之 :zmq_null - 无安全和加密

    ZeroMQ 官方地址 :http://api.zeromq.org/4-2:zmq_null zmq_null(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_null - 无 ...

  7. 【hihoCoder】1033: 交错和

    初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...

  8. HTML 5 视频(video)

    video 元素支持三种视频格式 IE Firefox Opera Chrome Safari 带有 Theora 视频编码和 Vorbis 音频编码的 Ogg 文件 No 3.5+ 10.5+ 5. ...

  9. 20145215实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...

  10. Oracle EBS - AOL

    AOL: (Path: /u43/dev6/interface/aol) 1. Goto system administrator response 2. View -> Request (Sa ...