每次听到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);
//接下来就可以做你喜欢的操作啦啦,是不是很简单
}
}