Alex
Alex

Reputation: 7612

Set 2 actions for UIBarButtonItem

Is it possible to register 2 actions for a UIBarButtonItem?

Specifically, I am referring to the "Cancel" UIBarButtonItem present in ABPersonViewController. This button has a default action behind it, which I don't want to change, but I would like to add a new action for when this button is pressed.

Any ideas on how this could be done?

Thanks!

Upvotes: 1

Views: 1038

Answers (2)

XJones
XJones

Reputation: 21967

I removed my previous answer as I had misunderstood the question. Not sure if this app needs to be approved by apple b/c not sure that you can get a pointer to the cancel button in a "legal" way but I'll assume that you can get at the button.

When you first present the person view controller you can get it's default target and action from the target and action properties and save them. Then set them to a custom target and action. When your custom action is called, send the default action to the default target and then do whatever your custom behavior is.

That's the best idea I have, hope it works!

Upvotes: 1

Mark Granoff
Mark Granoff

Reputation: 16938

Assuming you can get a reference to the button, you should be able to call addTarget:action:forControlEvents: to add another action.

I discovered by accident that you could do this to a UIButton when I was (I thought) replacing actions on a button depending on the state of the UI, but I was in fact adding actions to buttons. This is when I learned about removeTarget:action:forControlEvents:. :-)

That all being said, if you have a button that says "Cancel", and you overload it with something extra that is user-visible, this might be confusing to the user. If you simply want something to happen in the background when the user taps cancel, then this is an interesting way to get it done! I like that.

Upvotes: 0

Related Questions