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?

Leo
  • 24,596
  • 11
  • 71
  • 92
androidnotgenius
  • 417
  • 1
  • 7
  • 17

2 Answers2

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!

halfer
  • 19,824
  • 17
  • 99
  • 186
Pawel
  • 4,699
  • 1
  • 26
  • 26
4

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

BAndonovski
  • 3,309
  • 1
  • 17
  • 19