7

So far, I've been using

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

which adds an additional action (I think). How can I replace the initial action with a new one?

2

2 Answers 2

14

You can remove a target for a specific action like this:

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

Or, better yet, you can remove all targets from your button like this:

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

Then, you can add your new target action:

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

That's pretty much it!

4

You first have to remove the current action with removeTarget:action:forControlEvents: and then add the new one with addTarget:action:forControlEvents:.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.