Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace the existing action of a(n?) UIButton?

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?

like image 926
androidnotgenius Avatar asked May 31 '10 18:05

androidnotgenius


2 Answers

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!

like image 200
Pawel Avatar answered Oct 22 '22 21:10

Pawel


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

like image 20
BAndonovski Avatar answered Oct 22 '22 22:10

BAndonovski