iOS中关联对象的简单使用(objc_setAssociatedObject)
来源:互联网 发布:淘宝手机详情添加视频 编辑:程序博客网 时间:2024/05/22 03:28
首先看一下此方法接收的参数
objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
1.被关联的对象,下面举的例子中关联到了UIAlertView
2.要关联的对象的键值,一般设置成静态的,用于获取关联对象的值
3.要关联的对象的值,从接口中可以看到接收的id类型,所以能关联任何对象
4.关联时采用的协议,有assign,retain,copy等协议,具体可以参考官方文档
下面就以UIAlertView为例子简单介绍一下使用方法。
使用场景:在UITableView中点击某一个cell,这时候弹出一个UIAlertView,然后在UIAlertView消失的时候获取此cell的信息,我们就获取cell的indexPath好了。
第一步:
import < objc/runtime.h>
static char kUITableViewIndexKey;- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ...... UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"这里是xx楼" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil]; //然后这里设定关联,此处把indexPath关联到alert上 objc_setAssociatedObject(alert, &kUITableViewIndexKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [alert show];}
第二步:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { NSIndexPath *indexPath = objc_getAssociatedObject(alertView, &kUITableViewIndexKey); NSLog(@"%@", indexPath); }}
结束!!!
0 0
- iOS中关联对象的简单使用(objc_setAssociatedObject)
- iOS中关联对象的简单使用(objc_setAssociatedObject)
- iOS中关联对象的简单使用objc_setAssociatedObject
- iOS中关联对象的简单使用objc_setAssociatedObject
- iOS关联(objc_setAssociatedObject)
- objc_setAssociatedObject 关联的使用
- 【iOS开发技术】对象关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 【iOS开发技术】对象关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- IOS中的关联对象实现objc_setAssociatedObject
- objc_setAssociatedObject的简单使用
- 对象关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [IOS] 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- iOS关联,objc_getAssociatedObject, objc_setAssociatedObject
- OC对象关联~objc_setAssociatedObject/objc_getAssociatedObject
- iOS-关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- iOS 关联对象的使用
- objc_setAssociatedObject与Block的简单使用
- objc_setAssociatedObject与Block的简单使用
- spark core源码分析3 Master HA
- Android初学习 - 在Service里面启动Activity
- Windows FILETIME与JAVA Date互转
- 【编程之美】任意给定一个32位无符号整数n,求n的二进制表示中1的个数
- 【leetCode】Maximum Gap
- iOS中关联对象的简单使用(objc_setAssociatedObject)
- 读书笔记之linux/unix系统编程手册(25)
- maven profile 简介
- ucosII移植:可重入代码
- 使用VNC远程连接windows2003/2008操作系统
- Html5新增小元素:figure与figurecaption address progress meter mark details与summary
- Rabbit 使用笔记(1)-helloworld
- busybox init进程分析
- Spring MVC事务配置