1

I have 1 button and 2 methods. One method calls this:

[button addTarget:self action:@selector(action1) forControlEvents:(UIControlEvents)UIControlEventTouchDown];

And the other calls this:

[button addTarget:self action:@selector(action2) forControlEvents:(UIControlEvents)UIControlEventTouchDown];

For some reason, the button's action will not change. I am sure I am calling the above code correctly. Is it trying to add an action to the button and making it call both functions? If so, how can I stop this from happening? I have tried releasing and setting the button to nil before setting the new action and no luck. Thanks for the help.

Preston
  • 1,039
  • 3
  • 12
  • 22

1 Answers1

2

Yes. It's actually calling action1 and action2. You need to call removeTarget before adding the new target on the button.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • Does this look good or do I need to add the action to remove? Will this remove all the actions attached to it? [button removeTarget:self action:nil forControlEvents:(UIControlEvents)UIControlEventTouchDown]; – Preston Feb 22 '11 at 02:06
  • 1
    @Preston: yes, by using `nil` you are telling **CocoaTouch** to remove all actions from target. – Pablo Santa Cruz Feb 22 '11 at 02:17