2

I need to remove an action from a object and then add a new one.

I've used this code to add the new action:

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

I've then tried to use this code to remove the old action:

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

The problem is that it somehow also removes the newAction.

Any ideas?

Thanks in advance :)

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
Eksperiment626
  • 985
  • 3
  • 16
  • 30

2 Answers2

5

In that case, a simple solution is to instead remove the old action first before adding the new one. i.e. do it the other way around.

You can remove all actions by passing nil for target too.

Ken
  • 30,811
  • 34
  • 116
  • 155
  • Tank you so much :)... don't know why i didn't think of that... and thanks for the nil tip, that sure made my future coding much simpler :) – Eksperiment626 Sep 09 '11 at 00:33
  • 2
    passing nil to the target will not result in remove all actions: parameter: target The target object—that is, the object to which the action message is sent. If this is nil, the responder chain **is searched for an object willing to respond to the action message**. – AndyPipkin Sep 18 '13 at 13:58
1

You can remove the action from a UIButton for example like this:

[self.myButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
Marcos Reboucas
  • 3,409
  • 1
  • 29
  • 35