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

转载:http://blog.csdn.net/u014220518/article/details/52873164

一.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.导入头文件#import2.新建一个Action的Category;然后在.h中添加一个事件的Block,代码如下:[objc] view plain copy 在CODE上查看代码片派生到我的代码片#importtypedef void (^ActionBlock)(UIButton *button);

@interface UIButton (Action)

@property (nonatomic,copy) ActionBlock actionBlock;

+ (UIButton *)createBtnWithFrame:(CGRect)frame title:(NSString *)title actionBlock:(ActionBlock)actionBlock;

@end

在这里我们创建了一个加方法,来创建一个按钮;

实现如下:

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

+ (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;

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

static NSString *keyOfMethod;

static NSString *keyOfBlock;

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

- (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.通过类别创建两个按钮

[objc] view plain copy 在CODE上查看代码片派生到我的代码片

- (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];

}




UIButton的扩展.h文件#importtypedef void (^btnBlock)();@interface UIButton (Block)- (void)handelWithBlock:(btnBlock)block;@end.m文件#import "UIButton+Block.h"#importstatic const char btnKey;

@implementation UIButton (Block)

- (void)handelWithBlock:(btnBlock)block

{

if (block)

{

objc_setAssociatedObject(self, &btnKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

[self addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];

}

- (void)btnAction

{

btnBlock block = objc_getAssociatedObject(self, &btnKey);

block();

}

@end

你可能感兴趣的:(iOS-OC-Runtime使用小谈(objc_setAssociatedObject))