iOS-OC-Runtime使用小谈(objc_setAssociatedObject)

释放双眼,带上耳机,听听看~!

一.objc/runtime.h相关
Objecitve-C的重要特性是Runtime(运行时),在Interacting with the Runtime(交互运行)中,运行时函数部分,苹果给出了/usr/lib/libobjc.A.dylib库,这个共享库提供支持动态属性的objective – c语言,通过其接口,可以用于开发将其他语言运行于Objective-C上的中间层(桥接层),库里的函数定义为纯C语言。
二.objc/runtime.h使用
在这里我们主要举个简单的栗子��,通过新建一个UIButton的类别。
1.导入头文件

import <objc/runtime.h>

2.新建一个Action的Category;
然后在.h中添加一个事件的Block,代码如下:

#import <UIKit/UIKit.h>  
  
typedef void (^ActionBlock)(UIButton *button);  
  
@interface UIButton (Action)  
  
@property (nonatomic,copy) ActionBlock actionBlock;  
  
+ (UIButton *)createBtnWithFrame:(CGRect)frame title:(NSString *)title actionBlock:(ActionBlock)actionBlock;  
  
@end  

在这里我们创建了一个加方法,来创建一个按钮;
实现如下:

+ (UIButton *)createBtnWithFrame:(CGRect)frame title:(NSString *)title actionBlock:(ActionBlock)actionBlock{  
    UIButton *button = [[UIButton alloc]init];  
    button.frame = frame;  
    [button setTitle:title forState:UIControlStateNormal];  
    [button addTarget:button action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];  
      
    objc_setAssociatedObject (button , &keyOfMethod, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);  
      
    return button;  
}  

objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
id object :表示关联者,是一个对象,变量名理所当然也是object
const void *key :获取被关联者的索引key
id value :被关联者,这里是一个block
objc_AssociationPolicy policy : 关联时采用的协议,有assign,retain,copy等协议,一般使用OBJC_ASSOCIATION_RETAIN_NONATOMIC
注意这里面我声明了两个索引KEY:

static NSString *keyOfMethod;  
static NSString *keyOfBlock;  
- (void)buttonClick:(UIButton *)button{  
      
    //通过key获取被关联对象  
    //objc_getAssociatedObject(id object, const void *key)  
    ActionBlock block1 = (ActionBlock)objc_getAssociatedObject(button, &keyOfMethod);  
    if(block1){  
        block1(button);  
    }  
      
    ActionBlock block2 = (ActionBlock)objc_getAssociatedObject(button, &keyOfBlock);  
    if(block2){  
        block2(button);  
    }  
}  
  
- (void)setActionBlock:(ActionBlock)actionBlock{  
    objc_setAssociatedObject (self, &keyOfBlock, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC );  
}  
  
- (ActionBlock)actionBlock{  
    return objc_getAssociatedObject (self ,&keyOfBlock);  
}  

3.通过类别创建两个按钮

- (void)uiConfig{  
    //第一个按钮  
    UIButton *button1 = [UIButton createBtnWithFrame:CGRectMake((ScreenWidth - 100)/2, (ScreenHeight - 50)/2 - 50, 100, 50) title:@"按钮" actionBlock:^(UIButton *button) {  
        float r = random()%255/255.0;  
        float g = random()%255/255.0;  
        float b = random()%255/255.0;  
        self.view.backgroundColor = RGB(r, g, b);  
    }];  
    button1.backgroundColor = [UIColor lightGrayColor];  
    [self.view addSubview:button1];  
      
    //第二个按钮  
    UIButton *button2 = [UIButton createBtnWithFrame:CGRectMake((ScreenWidth - 100)/2, CGRectGetMaxY(button1.frame) + 50, 100, 50) title:@"按钮2" actionBlock:nil];  
    button2.actionBlock = ^(UIButton *button){  
        NSLog(@"---%@---",button.currentTitle);  
    };  
    button2.backgroundColor = [UIColor lightGrayColor];  
    [self.view addSubview:button2];  
}  

4.获取苹果私有的APi
这里我们也可以通过runtime获取苹果私有的方法;

#pragma mark - 获取苹果私有的APi方法  
- (void)getiOSPrivateAPi{  
    NSString *className = NSStringFromClass([UIView class]);  
      
    const charchar *cClassName = [className UTF8String];  
      
    id theClass = objc_getClass(cClassName);  
      
    unsigned int outCount;  
      
    Method *m =  class_copyMethodList(theClass,&outCount);  
      
    NSLog(@"%d",outCount);  
      
    for (int i = 0; i<outCount; i++) {  
          
        SEL a = method_getName(*(m+i));  
          
        NSString *sn = NSStringFromSelector(a);  
          
        NSLog(@"%@",sn);  
    }  
}  

注意,这里如果你想看其他的类,你把UIView换成你想要的类就好了;5.DEMO下载地址
http://download.csdn.net/detail/u014220518/9659110

人已赞赏
iOS文章

IOS应用抓取HTML网页数据

2019-9-19 15:15:04

iOS文章

iOS-tabbar控制从首页内容跳转到第三个Controller

2019-9-19 15:31:18

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索