原文地址

http://blog.csdn.net/lengshengren/article/details/16886915

//唯一静态变量key

static const char associatedkey;

static const char associatedButtonkey;

- (IBAction)sendAlert:(id)sender

{

NSString *message =@"我知道你是按钮了";

UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"我要传值·"
delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];

alert.delegate =self;

[alert show];

//#import <objc/runtime.h>头文件

//objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。

//1
源对象alert

//2
关键字 唯一静态变量key associatedkey

//3
关联的对象 sender

//4
关键策略  OBJC_ASSOCIATION_RETAIN_NONATOMIC

objc_setAssociatedObject(alert, &associatedkey, message,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

objc_setAssociatedObject(alert, &associatedButtonkey, sender,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

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

{

//通过 objc_getAssociatedObject获取关联对象

NSString  *messageString =objc_getAssociatedObject(alertView, &associatedkey);

UIButton *sender = objc_getAssociatedObject(alertView, &associatedButtonkey);

_labebutton.text = [[sendertitleLabel]text];

_ThisLabel.text = messageString;

//使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。

}

demo http://download.csdn.net/detail/lengshengren/6594365

 

通过 objc_setAssociatedObject alert 和 button关联 及传值的更多相关文章

  1. (十九)TableView的点击监听和数据刷新(Alert的多种样式) -tag传值的技巧

    要实现监听,要使用代理,控制器要成为TableView的代理. 注意下面的方式是代理方法: - (void)tableView:(UITableView *)tableView didSelectRo ...

  2. objc_setAssociatedObject 1

    [Objective-C]关联(objc_setAssociatedObject.objc_getAssociatedObject.objc_removeAssociatedObjects) 标签:  ...

  3. objc_setAssociatedObject

    学习笔记:通过 objc_setAssociatedObject alert 和 button关联 及传值 标签: ios 2013-11-22 16:25 7924人阅读 评论(1) 收藏 举报   ...

  4. iOS-runtime-objc_setAssociatedObject(关联对象以及传值)

    例子: static const char kRepresentedObject; - (IBAction)doSomething:(id)sender { UIAlertView *alert = ...

  5. ios 关联对象运用 objc_setAssociatedObject

    点按钮的时候,给alertView添加一个关联对象(被点击这个按钮), objc_setAssociatedObject(alert, &kRepresentedObject, sender, ...

  6. Runtime应用(二)使用对象关联为分类增加属性(每个对象的属性互不干扰)

    一.对象的关联方法有 1. void objc_setAssociatedObject(id object, const void *key, id value,objc_AssociationPol ...

  7. objc_setAssociatedObject 使用(转)

    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 ...

  8. WebForm跨页面传值---内置对象

    一.Response Response - 响应请求对象 string path = "Default2.aspx": (1)Response.Redirect(path); -- ...

  9. obj-c编程19:关联对象

    对于一些无法子类化的实例对象来说,如果希望将一个对象与其绑定该如何做呢? 以下示例虚构了一个HyConsoleAlert类,User类将会使用该类在控制台显示定制的告警.如果User中包括多个Aler ...

随机推荐

  1. AFNetworking 和 ASIHTTPRequest

    在开发iOS应用过程中,如何高效的与服务端API进行数据交换,是一个常见问题.一般开发者都会选择一个第三方的网络组件作为服务,以提高开发效率和稳定性.这些组件把复杂的网络底层操作封装成友好的类和方法, ...

  2. user_admin

    # -*- coding:utf-8 -*- from django.contrib import admin from django.contrib.auth.models import User ...

  3. IE浏览器连不上网,其他浏览器可以

    周末因工作需要,需用IE浏览器.结果发现IE连不上网,而其他浏览器正常上网. 首先排查不是网络连接问题. 又重启了一下网络连接.禁用---->启用. 还是不好使.(最后找到原因是DNS设置问题. ...

  4. 谷歌机器学习速成课程---2深入了解机器学习(Descending into ML)

    1.线性回归 人们早就知晓,相比凉爽的天气,蟋蟀在较为炎热的天气里鸣叫更为频繁.数十年来,专业和业余昆虫学者已将每分钟的鸣叫声和温度方面的数据编入目录.Ruth 阿姨将她喜爱的蟋蟀数据库作为生日礼物送 ...

  5. PAT 天梯赛 L1-028. 判断素数 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...

  6. Linux服务器iops性能测试-iozone

    1. 选用工具: iozone           下载地址:http://www.iozone.org/ (直接下载rpm包) 2. 工具安装: 执行命令: rpm -ivh iozone-3-40 ...

  7. 转:USB枚举

  8. String和StringBufffer的区别

    string的字符串操作都是废弃已有的对象,开辟一个新的内存空间创建一个新的对象 比如一个string str= "字符串"; str += "a"; 这样的操 ...

  9. 前端常用js脚本

    常用js整理 //获取Url中的参数值 function getQueryString(name) { var reg = new RegExp("(^|&)" + nam ...

  10. 物理分辨率与逻辑分辨率,pt与px

    有些小伙伴们,在使用chrome的移动端调试工具调试网页的时候,会发现iphone6上的尺寸为375*667,不禁差异,iphone6的分辨率不是750*1334吗? 实际上调试器上的大小单位不是px ...