views:

714

answers:

3

Is there a way I can programmatically change the IBAction of a UIButton?

I know I can just create two buttons and use button.hidden = BOOL, but I'd much rather just change the IBAction itself.

+2  A: 

I'm not sure I understand the question? Why not have the IBAction call a custom method and put some logic about which action to take in that method?

Mobs
because why do it with your own logic when Apple provides the logic for you :)
Matt S.
I don't think I would consider hooking up and removing IBActions as 'logic'.And if you're considering the MVC model, I would think this would be a better solution to your problem.
ACBurk
+7  A: 

Sure. You can use addTarget:forControlEvents: and removeTarget:action:forControlEvents: to manage the connections between action methods and controls manually. Both methods are in UIControl.

St3fan
ok, so I would call [self addTarget:button action:theIBAction forControlEvents:UIControlEventTouchDown];
Matt S.
[button addTarget:self action:theIBAction forControlerEvents:UIControlEventTouchDown], assuming that self responds to theIBAction
Thomas Müller
I'm getting an error when I set the IBAction, says it isn't declared...
Matt S.
+1  A: 

Sure:

-removeTarget:action:forControlEvents: to remove the old action

-addTarget:action:forControlEvents: to add the new action

Thomas Müller