ios - runtime运行时应用---交换方法
- 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运行时应用---交换方法的更多相关文章
- iOS runtime 运行时( 二 )
我们在编程过程中,如果使用到了runtime(运行时),我们几乎都是想动态的改变这个类的信息,包括方法,属性,balabala的,并且获得这个类的一些信息,等等,下面我们就来看看怎么通过runtime ...
- Objective-C Runtime 运行时之三:方法与消息
基础数据类型 SEL SEL又叫选择器,是表示一个方法的selector的指针,其定义如下: typedef struct objc_selector *SEL; objc_selector结构体的详 ...
- Objective-C Runtime 运行时之三:方法与消息(转载)
前面我们讨论了Runtime中对类和对象的处理,及对成员变量与属性的处理.这一章,我们就要开始讨论Runtime中最有意思的一部分:消息处理机制.我们将详细讨论消息的发送及消息的转发.不过在讨论消息之 ...
- iOS runtime 运行时( - )
谈到运行时,相对应的就有编译时: 1).运行时-- 直到程序运行时才去确定一个对象的具体信息,并且可以改变这个类的具体信息,包括它的方法,变量等等: 2).编译时-- 是在程序运行之前,编译的时候,就 ...
- iOS runtime 运行时( 三 )
上一篇 聊的是: 在不知道一个对象有什么属性的情况下, 获取对象的所有属性,然后可以改变属性的值, 今天我们谈谈 怎么通过Category(类别)+runtime 给一个类 添加一个新的属性,一般情况 ...
- iOS开发——高级特性&Runtime运行时特性详解
Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...
- Runtime运行时的那点事儿
注:本文是对 Colin Wheeler 的 Understanding the Objective-C Runtime 的翻译. 初学 Objective-C(以下简称ObjC) 的人很容易忽略一个 ...
- runtime 运行时机制 完全解读
runtime 运行时机制 完全解读 目录[-] import import 我们前面已经讲过一篇runtime 原理,现在这篇文章主要介绍的是runtime是什么以及怎么用!希望对读者有所帮助! ...
- 编译器设计-RunTime运行时环境
编译器设计-RunTime运行时环境 Compiler Design - Run-Time Environment 作为源代码的程序仅仅是文本(代码.语句等)的集合,要使其活动,它需要在目标计算机上执 ...
随机推荐
- 试听笔记:javascript入门精通
一.数据类型 1.原始类型:number.string.boolean.null.undefined 2.对象类型:Object (Function.Array.Date...) P.类型隐式转换:' ...
- LINQ绑定List到GridView
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 清华学堂 Range
Descriptioin Let S be a set of n integral points on the x-axis. For each given interval [a, b], you ...
- php 小函数
1 变量函数 a.is_xxx函数用来判断变量类型 is_numeric (PHP 4, PHP 5) — 检测变量是否为数字或数字字符串 is_int.is_integer.is_long,判断变 ...
- 【贪心】SOJ 13983
SOJ 13983. Milk Scheduling 这是比赛题,还是作死的我最讨厌的英文题,题目大意就是有n头奶牛,要在喂奶截止时间前给他喂奶并得到相应的含量的牛奶. 一开始的想法就是挑选截止日期的 ...
- How To Handle a Loss of Confidence in Yourself
Do you feel like you've lost confidence in yourself? Have you had strong self doubts? Perhaps you we ...
- <三>JDBC_面向对象思想的体现
JDBCTools.java import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;i ...
- C# XMPP客户端与openfire通信(Matrix Xmpp 授权破解教程)
如此著名的XMPP , 居然试过jabber-net, agsXmpp,matrix Xmpp, 无数多个例子, 居然没有一个能实现最简单的 发消息.收消息功能.好无语. 最后还是决定从商业产品 Ma ...
- MySQL Replication需要注意的问题
MySQL Replication 大家都非常熟悉了,我也不会写怎么搭建以及复制的原理,网上相关文章非常多,大家可以自己去搜寻.我在这里就是想总结一下mysql主从复制需要注意的地方.有人说主从复制很 ...
- iOS一些编译运行问题
1.在运行工程时,出现错误如下所示: Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_XXXXX", refer ...