博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文章标题
阅读量:4129 次
发布时间:2019-05-25

本文共 3253 字,大约阅读时间需要 10 分钟。

关于runtime的 class_addMethod 和 method_exchangeImplementations方法

////  UIBarButtonItem+Public.m//  RuntimeDemo////  Created by huokr on 2/27/14.//  Copyright (c) 2014 huokr. All rights reserved.//#import "UIBarButtonItem+Public.h"#import 
@implementation UIBarButtonItem (Public)/**/- (id)initWithCustomTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action{ ___start_log___ self = [super init]; if (self) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; CGRect frame = button.frame; if (title) { CGSize size = [title sizeWithFont:[UIFont boldSystemFontOfSize:16.0f] constrainedToSize:CGSizeMake(MAXFLOAT, 16.0f) lineBreakMode:(NSLineBreakByCharWrapping)]; [button setTitle:title forState:(UIControlStateNormal)]; [button setTitleColor:[UIColor purpleColor] forState:(UIControlStateNormal)]; button.titleLabel.font = [UIFont systemFontOfSize:16.0f]; if (size.width > 40) { frame.size.width = size.width + 10; button.frame = frame; } } button.backgroundColor = [UIColor clearColor]; [button addTarget:target action:action forControlEvents:(UIControlEventTouchUpInside)]; self = [[UIBarButtonItem alloc] initWithCustomView:button]; } return self;}+ (void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class];//系统的原方法// - (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action; SEL originalSelector = @selector(initWithTitle:style:target:action:); SEL swizzledSelector = @selector(initWithCustomTitle:style:target:action:); Method originalMethod = class_getInstanceMethod(class, @selector(initWithTitle:style:target:action:)); Method swizzledMethod = class_getInstanceMethod(class, @selector(initWithCustomTitle:style:target:action:)); //class_addMethod 参考 [这里写链接内容](http://longtimenoc.com/archives/ios%E5%9C%A8%E8%BF%90%E8%A1%8C%E6%97%B6%E4%B8%BA%E7%B1%BB%E6%B7%BB%E5%8A%A0%E6%96%B9%E6%B3%95) BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); if (didAddMethod) { ___start_log___ class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { //交换两个方法的实现! method_exchangeImplementations(originalMethod, swizzledMethod); } });}/**/@end

以上代码重载了load方法,其中利用oc 的 runtime机制把系统的initWithTitle:style:target:action:方法替换为自定义的initWithCustomTitle:style:target:action:

其中:
了解class_addMethod 参考
本代码原作者地址:

转载地址:http://qifvi.baihongyu.com/

你可能感兴趣的文章
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Count and Say
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
驱动力3.0,动力全开~
查看>>
记CSDN访问量10万+
查看>>
Linux下Oracle数据库账户被锁:the account is locked问题的解决
查看>>
记CSDN访问20万+
查看>>
Windows 环境下Webstorm 2020.3 版本在右下角找不到Git分支切换部件的一种解决方法
查看>>
Electron-Vue项目中遇到fs.rm is not a function问题的解决过程
查看>>
飞机换乘次数最少问题的两种解决方案
查看>>
有向无回路图的理解
查看>>