How to replace an existing (n?) UIButton action?

So far i have used

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

which adds an extra action (I think). How to replace the original action with a new one?

+7
iphone uibutton
source share
2 answers

First you need a link to your button1. Then you need to unregister the target by calling the following action:

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

You can pass nil as an action, this will delete all the actions related to this purpose using the button.

Then you need to call

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

That is pretty!

Hope this was helpful, Pawel

+13
source share

First you need to remove the current action using removeTarget: action: forControlEvents: and then add a new one using addTarget: action: forControlEvents :.

+3
source share

All Articles