2012-07-13 58 views
0

我正在UITableViewCell上創建自定義單選按鈕。我很困惑實施一個代表,它會通知我每個按鈕的選擇。通知代表有關iPhone中的自定義單選按鈕索引選擇

例如,在UITableView中,我們有一個方法:

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

當用戶選擇特定行調用它。我想爲我的選項按鈕選擇具有相同的行爲。

在此先感謝。

回答

1

我建議你的子類UIButton,並使用通常的目標/動作語義,而不是重新發明輪子。

但是,如果您想一般地實施委派模式,那麼在Cocoa Fundamentals Guide中有Apple的優秀文檔(向下滾動到標題爲「實現自定義類的委託」的部分)。

1

你會想要觀察用戶採取的行動。我將創建單選按鈕作爲UIControl,並使用目標操作來觀察用戶選擇。

所以你要告訴按鈕:

[radioButton addTarget:self action:@selector(radioButtonValueDidChange:) forControlEvent:UIControlEventValueChanged]; 

,然後實現一個遵守操作方法:

- (void)radioButtonValueDidChange:(RadioButton *)button { 
// Do whatever you need to after a user selects a button. 
} 

所以,現在的「radioButtonValueDidChange:」將每次叫對象被選中或取消選擇,你可以採取相應的行動。

+0

謝謝老兄,它幫了我很多。不過我用通用的方式和上面的方法裏面使用了委託模式。 – 2012-07-13 06:22:12

+1

設置表格單元格時,可以使用標記變量來區分哪個單選按鈕已更改。 radioButton.tag = indexPath.row; – 2012-07-13 07:38:49