2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
- (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] 0
2015-07-22 16:18:35.295 test[5174:144121] 你是谁
2015-07-22 16:18:35.295 test[5174:144121] 点我

最新文章

  1. ExecutorService线程池应用
  2. mysql创建表
  3. [Labview资料] labview事件结构学习
  4. git小技巧--提取/合并某分支的部分文件
  5. 【HDU 5572 An Easy Physics Problem】计算几何基础
  6. 了解 : prevent default
  7. Java中元组的使用
  8. python端口扫描器
  9. sublime text package control 被墙的解决办法
  10. 分布式架构真正适用于大型互联网项目的架构! dubbo+zookeeper+springmvc+mybatis+shiro+redis
  11. Tomcat 配置文件 server.xml
  12. springboot整合mybatics PLUS
  13. 如何对接oracle 建立pdb
  14. 【Selenium-WebDriver自学】Selenium-IDE工具特点(二)
  15. 接口测试xml格式转换成json
  16. Opencv 入门学习之图片人脸识别
  17. 设置SVN忽略文件和目录(文件夹)
  18. HTML小记
  19. Java经典23种设计模式之行为型模式(三)
  20. 互联网大规模数据分析技术(自主模式)第五章 大数据平台与技术 第10讲 大数据处理平台Hadoop

热门文章

  1. windbg学习—-.ecxr
  2. &lt;command-line&gt;:0: error: macro names must be identifiers
  3. css3的cursor
  4. javascript快速入门17--事件
  5. Java笔记2:Eclipse编写第一个Java程序
  6. k8s debug记录之kubelet user.slice container monitor failure
  7. 2017.12.07 postgresql使用with recursive完成迭代查询
  8. Win7如何修改开机动画
  9. require.js - 详解
  10. rap 实现分页效果