1

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!

Alex
  • 7,432
  • 20
  • 75
  • 118

2 Answers2

1

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!

XJones
  • 21,959
  • 10
  • 67
  • 82
  • XJones, not really. I created my own CustomABPersonViewController class which I open up. You are probably referring to ABNewPersonViewController, which only adds a new person. For ABPersonViewController, the Cancel button only returns the view to its normal/original form. I don't really see any other solution except adding 2 actions for a button. I have tried attaching just one action to the button, but that will ignore the default action. – Alex Apr 11 '11 at 16:59
  • Sorry, I had misunderstood. I revised my answer, hope it helps. – XJones Apr 11 '11 at 17:55
0

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.

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61
  • Thanks for the response Mark. However, there is no addTarget:action:forControlEvents: for a UIBarButtonItem. – Alex Apr 11 '11 at 17:28
  • Indeed. But there are individual `setTarget:` and `setAction:` methods. That makes me wonder if `UIBarButtonItems`'s support multiple actions like `UIButton`'s or not... Give it a try and let us know. – Mark Granoff Apr 11 '11 at 17:31
  • Already tried it Mark. UIBarButtonItems support only 1 target and action. If you "set" the action, it will overwrite whatever was there before (e.g. the default action from ABPersonViewController). I was thinking there might be a way to go around this with an observer, but I'm not having any luck with that so far. – Alex Apr 11 '11 at 17:35
  • Hmm.. Well, if tapping "Cancel" also pops the view, perhaps you could do whatever you need to in `viewWillDisappear:`. Alternatively, perhaps you could simply replace the provided Cancel button with a new button that you instantiate and control completely. – Mark Granoff Apr 11 '11 at 17:38
  • Cancel doesn't call viewWillDissappear, it just stops editing. I can't replace the "cancel" button, because then I don't know how to go from edit mode to normal mode. – Alex Apr 13 '11 at 17:02