如何替换(n?)UIButton的现有动作?

时间:2010-05-31 18:27:00

标签: iphone uibutton

到目前为止,我一直在使用

[button1 addTarget:self action:@selector(newAction) forControlEvents:UIControlEventTouchUpInside];

增加了一个额外的动作(我认为)。如何用新的操作替换初始操作?

2 个答案:

答案 0 :(得分:14)

您可以删除特定操作的目标,如下所示:

[button1 removeTarget: self action: @selector(oldAction) forControlEvents: UIControlEventTouchUpInside]

或者,更好的是,您可以删除按钮中的所有目标,如下所示:

[button1 removeTarget: nil action: NULL forControlEvents: UIControlEventAllEvents]

然后,您可以添加新的目标操作:

[button1 addTarget:self action:@selector(newAction) forControlEvents: UIControlEventTouchUpInside];

这就是它!

希望这有用, 的Pawel

答案 1 :(得分:3)

首先必须使用removeTarget:action:forControlEvents:删除当前操作,然后使用addTarget:action:forControlEvents:。

添加新操作。