iOS_关联使用(一)

来源:互联网 发布:linux c语言utf8转gbk 编辑:程序博客网 时间:2024/06/05 01:22

关联是指把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分。
关联特性只有在Mac OS X V10.6以及以后的版本上才是可用的。

在类的定义之外为类增加额外的存储空间

使用关联,我们可以不用修改类的定义而为其对象增加存储空间。这在我们无法访问到类的源码的时候或者是考虑到二进制兼容性的时候是非常有用。

关联是基于关键字的,因此,我们可以为任何对象增加任意多的关联,每个都使用不同的关键字即可。关联是可以保证被关联的对象在关联对象的整个生命周期都是可用的(在垃圾自动回收环境下也不会导致资源不可回收)。

创建关联

创建关联要使用到Objective-C的运行时函数:objc_setAssociatedObject来把一个对象与另外一个对象进行关联。该函数需要四个参数:源对象,关键字,关联的对象和一个关联策略。当然,此处的关键字和关联策略是需要进一步讨论的。
    ■  关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
    ■  关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。这种关联策略         是通过使用预先定义好的常量来表示的。

   关联是指把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分。

   关联特性只有在Mac OS X V10.6以及以后的版本上才是可用的。


Block关联使用:


CustomView类的.h文件

#import <UIKit/UIKit.h>

typedef void (^BASE_BLOCK_NIL)();

@interface CustomView : UIView

- (id)initWithFrame:(CGRect)frame addBlock:(BASE_BLOCK_NIL)aLoadBlock;

@end


CustomView类的.m文件

#import "CustomView.h"

#import <objc/runtime.h>

static NSString *BUTTON_LOAD_BLOCK = @"load_button_block";

@implementation CustomView


- (id)initWithFrame:(CGRect)frame addBlock:(BASE_BLOCK_NIL)aLoadBlock{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor brownColor];

        

        objc_setAssociatedObject(self, &BUTTON_LOAD_BLOCK, aLoadBlock, OBJC_ASSOCIATION_COPY);

        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(100, 100, 50, 50);

        button.backgroundColor = [UIColor purpleColor];

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

        [self addSubview:button];


        UIButton *buttonRemove = [UIButton buttonWithType:UIButtonTypeSystem];

        buttonRemove.frame = CGRectMake(100, 200, 50, 50);

        buttonRemove.backgroundColor = [UIColor orangeColor];

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

        [self addSubview:buttonRemove];

    }

    return self;

}


- (void)buttonAction:(id)sender{

    BASE_BLOCK_NIL block = objc_getAssociatedObject(self, &BUTTON_LOAD_BLOCK);

    if (block) {

        NSString *str = @"关联_Block传值";

        block(str);

    }

}


- (void)buttonRemoveAction:(id)sender{/*取消关联*/

    objc_removeAssociatedObjects(self);

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

@end


ViewController类的.m文件

#import "ViewController.h"

#import "CustomView.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    CustomView *aView = [[CustomView alloc] initWithFrame:self.view.bounds addBlock:^(id str){

        NSLog(@"传值^^^^^%@",str);

    }];

    [self.view addSubview:aView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end





0 0
原创粉丝点击