原文地址

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. django自带的用户认证和form表单功能

    一.用户认证 1.用户认证方法 1.ajango自带用户认证功能,只需要引入相应的模块就可以使用,但是前提是必须使用ajango自带的auth_user表,并且需要把用户相关信息存放在该表中. 2.引 ...

  2. java eclipse 监视选择指定变量

    http://3y.uu456.com/bp_8tzmk3zobb7k6x46aj28_1.html 有时一个Java程序有许多变量,但你仅对其中一个或几个感兴趣,为了监视选择的变量和表达式,你可以将 ...

  3. LeetCode:整数转罗马数字【12】

    LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...

  4. Tensorflow学习笔记(1)--安装

    安装 1.ubuntu 14.04 2. 清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/ (要求sudo权限,如果报 ...

  5. Loadrunder常见问题汇总(持续更新)

    1.LR 脚本为空的解决方法: 1)如果安装了IE以外的浏览器,并且IE不是默认浏览器,则无法生成录制脚本 2)如果录制脚本时IE不能打开,则需要将浏览器的IE工具高级选项中,将“启用第三方浏览器扩展 ...

  6. 转:MFC中屏蔽ESC和回车关闭对话框

    解决方法是在 CDialog::PreTranslateMessage() 的重载函数中将ESC和回车按键的消息处理掉. 直接上代码: CResultCollectorDlg::PreTranslat ...

  7. Cocos2d-x项目移植到WP8系列之一:前传

    原文链接: http://www.cnblogs.com/zouzf/p/3969993.html 许久没动笔了,随想一直都有动笔的想法,但拖来拖去,归根到底还是一个懒字吧 .发现人的惰性真是太强大了 ...

  8. VCFtools

    The C++ executable module examples This page provides usage examples for the executable module. Exte ...

  9. python去掉行尾的换行符

    python去掉行尾的换行符 mystring.strip().replace(' ', '').replace('\n', '').replace('\t', '').replace('\r', ' ...

  10. jsp路径问题

    绝对路径:/StudentInfo/images/login.jpg 相对路径:images/login.jpg 路径前面的第一个/代表tomcate目录下面的webapps这个文件夹 jsp的Adv ...