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