ios runtime 动态属性objc_setAssociatedObject和objc_getAssociatedObject
2016-04-11 15:25
477 查看
// // ViewController.m // objc_runtime // // Created by 王飞 on 16/4/11. // Copyright © 2016年 com.startchina. All rights reserved. // #import "ViewController.h" #import <objc/runtime.h> @interface ViewController () @end @implementation ViewController - (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]; // Do any additional setup after loading the view, typically from a nib. } -(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可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- Preference Learning——Object Ranking
- Jackson 高性能的JSON处理 ObjectMapper
- Objective-C 之Runtime消息机制
- Objective-C 与 C++ 的区别
- iOS开发之Objective-c的MD5/SHA1加密算法的实现
- Objective-C利用AFN上传头像、UIAlertController的使用
- Objective-C的对象模型和runtime机制
- Objective-C语法之KVO使用
- "A MultiPath Network for Object Detectio" 阅读笔记
- ArrayList的contains方法与Object的equals方法的关系
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- 'ModelOptions' object has no attribute 'get_field_names
- Object -c 单例模式
- iOS Objective-C基本核心动画,偏移,旋转,缩放,路径,抖动,组动画
- Swift项目中调用Objecttive-C
- 什么是writeObject 和readObject?可定制的序列化过程(转)
- iOS开发67- valueForKey:和objectForKey:的区别
- libcudnn.so.6.5:cannot open sharedobject file: No such file or directory
- Objective-C中nil与release的区别与用法
- objective-c中自己创建的对象为什么不能调用release