The role of OC in my view is to associate two objects, when used to take out (the project I do is associated with a property in the UITableView)
Give me a chestnut:
-(void) viewdidload {[Super viewdidload]; UIButton * button = [UIButton buttonwithtype:uibuttontypecustom]; Button.frame = CGRectMake (100, 100, 100, 100); [Button settitle:@ "Association test" Forstate:uicontrolstatenormal]; [Button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Button addtarget:self action: @selector (BUTTONTAP) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button];} -(void) buttontap{uialertview * alert = [[Uialertview alloc] initwithtitle:@ "" message:@ "How (IS) doing" delegate:self cancelbuttontitle:@ "not bad" otherbuttontitles:@ "Fine", nil]; void (^block) (Nsinteger) = ^ (Nsinteger buttonindex) {if (Buttonindex = = 0) {NSLog (@ "buttonindex:0"); }else{NSLog (@ "buttonindex:1"); } }; Objc_setassociatedobject (Alert, key, block, objc_association_retain_nonatomic); [Alert show]; }-(void) Alertview: (UIALERTVIEW *) Alertview Clickedbuttonatindex: (Nsinteger) buttonindex{Void (^block) (Nsinteger) = Objc_getassociatedobject ( Alertview, key); Block (Buttonindex);} -(void) didreceivememorywarning {[Super didreceivememorywarning]; } @end
The chestnut above is the link between alert and block, what type of object you are associated with, what object type is obtained, and then use the extracted object to do what you want to do, the chestnut above is to take out the block, done in the block do you want to do, and block behind alert , make you look at the code more easily, (in my opinion, and eggs), this chestnut is I from the "effective object-c" for reference, I really feel that this is not very good, but for understanding the object association enough, Objc_association_retain_ Nonatomic this and (Nonatomic,retain) is the same effect as the following table
| Objc_association_assign |
@property (assign) or @property (unsafe_unretained) |
Specifies a weak reference to the associated object. |
| Objc_association_retain_nonatomic |
@property (nonatomic, Strong) |
Specifies a strong reference to the associated object, and that the association are not made atomically. |
| Objc_association_copy_nonatomic |
@property (nonatomic, copy) |
Specifies the associated object is copied, and that the association are not made atomically. |
| Objc_association_retain |
@property (Atomic, Strong) |
Specifies a strong reference to the associated object, and that the association is made atomically. |
| Objc_association_copy |
@property (Atomic, copy) |
Specifies the associated object is copied, and that the association is made atomically. |
And, with block also pay attention to circular reference problem, this here is not much to say, I personally think http://kingscocoa.com/tutorials/associated-objects/this write good, is all English, but not difficult, Suggestions can see, understand the problem will be higher, this blog explains why with the association, raise the chestnut is normal write case, record the cell you want to delete the Indexpath, will this indexpath into a global variable to record, finished operation Delete, But to think of a problem is if I am in other places also operate this value will be confused, or I use once, with the global variable is too troublesome, so simply use the association record this value, so you can imitate me above the chestnut write, but Bo master with three methods elaborated, the first is to want me above chestnut as normal writing, The second is to NSObject extension method, the object and the associated value, the third is to Uialertview Add class method, the Indexpath set to attribute, rewrite setter and getter method, I think Bo Master idea is very ingenious, hope to learn from, thank you
Reference Link: http://nshipster.com/associated-objects/
http://kingscocoa.com/tutorials/associated-objects/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Object-c Associated Object