[Objective-C]關聯(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)

關聯是指把兩個對象相互關聯起來,使得其中的一個對象作為另外一個對象的一部分。

關聯特性只有在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需要四個參數:源對象,關鍵字,關聯的對象和一個關聯策略。

//1 源對象alert

//2 關鍵字 唯一靜態變量key associatedkey

//3 關聯的對象 sender

//4 關鍵策略? 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);

//把alert和message字符串關聯起來,作為alertview的一部分,關鍵詞就是msgstr,之后可以使用objc_getAssociatedObject從alertview中獲取到所關聯的對象,便可以訪問message或者btn了

//? ? 即實現了關聯傳值

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

}

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

{

//通過 objc_getAssociatedObject獲取關聯對象

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

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

NSLog(@"%ld",buttonIndex);

NSLog(@"%@",messageString);

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

//使用函數objc_removeAssociatedObjects可以斷開所有關聯。通常情況下不建議使用這個函數,因為他會斷開所有關聯。只有在需要把對象恢復到“原始狀態”的時候才會使用這個函數。

}

終端打印:

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] 點我

補充:該方法在button addtarget的時候@selecter只能傳一個sender。但是當想要傳一個字典或者數組的時候,就可以使用關聯的方法,將dict帶入到我們的其他方法中。并通過get方法取出這個dict對象

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容