objc_setAssociatedObject 使用
来源:互联网 发布:云计算工程师做什么 编辑:程序博客网 时间:2024/05/21 20:38
http://my.oschina.net/wupengnash/blog/482377
- (void)viewDidLoad { [super viewDidLoad]; // static const char associatedButtonkey; 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可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。}终端打印:2015-07-22 16:18:35.294 test[5174:144121] 02015-07-22 16:18:35.295 test[5174:144121] 你是谁2015-07-22 16:18:35.295 test[5174:144121] 点我
- objc_setAssociatedObject 使用
- objc_setAssociatedObject 使用
- objc_setAssociatedObject 使用
- objc_setAssociatedObject 使用
- objc_setAssociatedObject的使用
- objc_setAssociatedObject 关联的使用
- objc_setAssociatedObject的简单使用
- objc_setAssociatedObject
- objc_setAssociatedObject
- objc_setAssociatedObject
- objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAsso使用场景
- 关于运行时的方法使用:objc_setAssociatedObject
- category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
- category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
- category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
- category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
- 关于运行时的方法使用:objc_setAssociatedObject
- category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
- rz fail over expect auto login ssh
- 每天进步一点点——Linux中的文件描述符与打开文件之间的关系
- 一个带标签的自定义控件 都可以改变 很方便
- junit学习之junit的基本介绍
- mysql5.6 centos7下安装之rpm安装
- objc_setAssociatedObject 使用
- 指针常量和指向常量的指针
- HDU-1698-Just a Hook(Lazy算法)
- python3的requests抓取中文页面出现乱码
- 7.30Oracle Net管理错误
- Linux 进程间通信
- android genymotion下载地址
- 秒杀系统架构分析与实战
- TextView文字旋转