通知代表iPhone中的自定义单选按钮索引选择

时间:2012-07-13 05:43:40

标签: iphone ios5 xcode4

我正在UITableViewCell上创建一个自定义单选按钮。我很难实现一个委托,它将通知我每个按钮的选择。

例如,在UITableView中,我们有一个方法:

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

当用户选择特定行时调用它。我希望我的选项按钮选择具有相同的行为。

提前致谢。

2 个答案:

答案 0 :(得分: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:”,你可以采取相应的行动。

答案 1 :(得分:1)

我建议你继承UIButton并使用通常的目标/动作语义,而不是重新发明轮子。

但是,如果您想要一般性地实现委托模式,那么在Cocoa Fundamentals Guide中有来自Apple的优秀文档(向下滚动到标题为“为自定义类实现委托”的部分)。