Ios_ Association Use (i)

Source: Internet
Author: User

Association refers to associating two objects together so that one of the objects is part of another object.
The affinity attribute is only available on Mac OS X V10.6 and later versions.

add extra storage space for a class beyond the definition of the classwith correlation, we can add storage space to objects without modifying the definition of the class. This is useful when we are unable to access the source code of the class or when considering binary compatibility. Associations are based on keywords, so we can add any number of associations to any object, each with a different keyword. An association is a guarantee that the associated object is available throughout the life cycle of the associated object (and does not cause the resource to be non-recyclable in a garbage-collected environment).

Create an association

Create an association run-time function to use to OBJECTIVE-C: Objc_setassociatedobject to associate an object with another object. The function requires four parameters: the source object, the keyword, the associated object, and an associated policy. Of course, the keywords and associated policies here are needed for further discussion.
The keyword is a type of void pointer. Each associated keyword must be unique. Static variables are usually used as keywords.
The association policy indicates whether the related object is associated by assignment, preserving the reference or copying it, and whether the association is atomic or non-atomic. The correlation policy here is similar to the Declaration property. This association strategy is represented by the use of a pre-defined represented.

Association refers to associating two objects together so that one of the objects is part of another object.

The affinity attribute is only available on Mac OS X V10.6 and later versions.


Block Association using:


The. h file for the CustomView class

#import <UIKit/UIKit.h>

typedef void (^base_block_nil) ();

@interface Customview:uiview

-(ID) initWithFrame: (CGRect) frame addblock: (Base_block_nil) Aloadblock;

@end


. m files of the CustomView class

#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 = @ " correlated _block pass value ";

Block (str);

}

}


-(void) Buttonremoveaction: (ID) sender{/* disassociate */

Objc_removeassociatedobjects (self);

}

/*

Only override Drawrect:if perform custom drawing.

An empty implementation adversely affects performance during animation.

-(void) DrawRect: (cgrect) Rect {

Drawing Code

}

*/

@end


. m files of the Viewcontroller class

#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 (@ " transmit value ^^ ^^ ^:%@", str);

}];

[Self.view Addsubview:aview];

}


-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}


@end





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ios_ Association Use (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.