Runtime中关联对象的简单使用

  每次听到runtime这个词,都会觉得是高大上,其实使用起来也超级简单,下面我就为大家简单介绍一下,使用runtime中的关联对象在实际开发中的妙用。如下:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

//触发一个操作,弹出一个对话框UIAlertView

const void *keyword = "indexRow";

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"选中了某一行" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

objc_setAssociatedObject(alertView,keyword,indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

[alertView show];

}

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

if (buttonIndex != alertView.cancelButtonIndex) {

//获取当前选中的项目

const void *keyword = "indexRow";

id indexPath =  objc_getAssociatedObject(alertView, keyword);

//接下来就可以做你喜欢的操作啦啦,是不是很简单

}

}

  

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容