[Objective-C]關(guān)聯(lián)(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)

關(guān)聯(lián)是指把兩個(gè)對(duì)象相互關(guān)聯(lián)起來,使得其中的一個(gè)對(duì)象作為另外一個(gè)對(duì)象的一部分。

關(guān)聯(lián)特性只有在Mac OS X V10.6以及以后的版本上才是可用的。

- (void)viewDidLoad?

{? ? [super viewDidLoad]; ? ?

?? ? ? ? ? ? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];? ?

? ? ?[btn setTitle:@"按鈕" forState:UIControlStateNormal];? ?

?[self.view addSubview:btn];? ??

[btn setFrame:CGRectMake(50, 50, 50, 50)];? ?

?btn.backgroundColor = [UIColor redColor];? ??

? ? [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; ? ? ? ? ? }

-(void)click:(UIButton *)sender

{? ? NSString *message = @"你是誰";? ? ??

? UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"我要傳值·" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];??

? alert.delegate = self;? ??

[alert show];? ? ??

? //#import<objc/runtime.h>頭文件

//objc_setAssociatedObject需要四個(gè)參數(shù):源對(duì)象,關(guān)鍵字,關(guān)聯(lián)的對(duì)象和一個(gè)關(guān)聯(lián)策略。

//1 源對(duì)象alert

//2 關(guān)鍵字 唯一靜態(tài)變量key associatedkey

//3 關(guān)聯(lián)的對(duì)象 sender

//4 關(guān)鍵策略? OBJC_ASSOCIATION_ASSIGN

//? ? enum {

//? ? ? ? OBJC_ASSOCIATION_ASSIGN = 0,? ? ? ? ? 若引用/**< Specifies a weak reference to the associated object. */

//? ? ? ? OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.

//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *? The association is not made atomically. */

//? ? ? ? OBJC_ASSOCIATION_COPY_NONATOMIC = 3,? /**< Specifies that the associated object is copied.

//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *? The association is not made atomically. */

//? ? ? ? OBJC_ASSOCIATION_RETAIN = 01401,? ? ? /**< Specifies a strong reference to the associated object.

//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *? The association is made atomically. */

//? ? ? ? OBJC_ASSOCIATION_COPY = 01403? ? ? ? ? /**< Specifies that the associated object is copied.

//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *? The association is made atomically. */

//? ? };

objc_setAssociatedObject(alert, @"msgstr", message,OBJC_ASSOCIATION_ASSIGN);

//把a(bǔ)lert和message字符串關(guān)聯(lián)起來,作為alertview的一部分,關(guān)鍵詞就是msgstr,之后可以使用objc_getAssociatedObject從alertview中獲取到所關(guān)聯(lián)的對(duì)象,便可以訪問message或者btn了

//? ? 即實(shí)現(xiàn)了關(guān)聯(lián)傳值

objc_setAssociatedObject(alert, @"btn property",sender,OBJC_ASSOCIATION_ASSIGN);

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

//通過 objc_getAssociatedObject獲取關(guān)聯(lián)對(duì)象

NSString? *messageString =objc_getAssociatedObject(alertView, @"msgstr");

UIButton *sender = objc_getAssociatedObject(alertView, @"btn property");

NSLog(@"%ld",buttonIndex);

NSLog(@"%@",messageString);

NSLog(@"%@",[[sender titleLabel] text]);

//使用函數(shù)objc_removeAssociatedObjects可以斷開所有關(guān)聯(lián)。通常情況下不建議使用這個(gè)函數(shù),因?yàn)樗麜?huì)斷開所有關(guān)聯(lián)。只有在需要把對(duì)象恢復(fù)到“原始狀態(tài)”的時(shí)候才會(huì)使用這個(gè)函數(shù)。

}

終端打印:

2015-07-22 16:18:35.294 test[5174:144121] 0

2015-07-22 16:18:35.295 test[5174:144121] 你是誰

2015-07-22 16:18:35.295 test[5174:144121] 點(diǎn)我

補(bǔ)充:該方法在button addtarget的時(shí)候@selecter只能傳一個(gè)sender。但是當(dāng)想要傳一個(gè)字典或者數(shù)組的時(shí)候,就可以使用關(guān)聯(lián)的方法,將dict帶入到我們的其他方法中。并通過get方法取出這個(gè)dict對(duì)象

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容