UIButton-JLModelButton
Github地址
一個 UIButton 的分類,使用 Runtime 動態(tài)綁定使它能夠持有 model 屬性(id 類型),便于直接在點擊事件中抓取數(shù)據(jù)。
這是在一個項目需求中想到的,用于減少獲取 Button 對應(yīng)的 model 產(chǎn)生錯誤,同時減少可能出現(xiàn)的遍歷以提升效率。
例如:
頁面中有一組 Button ,可以將他們對應(yīng)的 model 直接賦值給 UIButton 對象,等我們需要獲取的時候,可以直接在 Button 的點擊事件當(dāng)中獲取到其對應(yīng) model 。
1.創(chuàng)建 Button 時將 Model 賦值給它
JLButtonModel *model = self.dataArr[i];
UIButton *btn = [[UIButton alloc] init];
btn.model = model;
[btn addTarget:self action:@selector(didTouchBtn:) forControlEvents:UIControlEventTouchUpInside];
2.在 Button 的點擊事件當(dāng)中直接通過屬性取值
- (void)didTouchBtn:(UIButton*)sender{
JLButtonModel *model = (JLButtonModel*)sender.model;
}
歡迎關(guān)注公眾號來分享知識

iOS技術(shù)公眾號320.jpeg