? 每次聽到runtime這個詞,都會覺得是高大上,其實使用起來也超級簡單,下面我就為大家簡單介紹一下,使用runtime中的關(guān)聯(lián)對象在實際開發(fā)中的妙用。如下:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//觸發(fā)一個操作,彈出一個對話框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) {
//獲取當(dāng)前選中的項目
const void *keyword = "indexRow";
id indexPath =? objc_getAssociatedObject(alertView, keyword);
//接下來就可以做你喜歡的操作啦啦,是不是很簡單
}
}
??