Search code examples
iphoneiosselectoruibarbuttonitemuitoolbar

Replace or extend UIBarButtonItem selector


I'm trying to make a custom UIToolbar which handles rotation and custom arrangement. When it's in portrait mode some of the barbuttonitems will not be visible, so I add a "more" button from which pops up a little view with these items. My problem is that when my current orientation is portrait and when I select a visible barbuttonitem ( which is not in popup ) I want to close the popup if it's open. I want the smae behavior for the uibarbuttons in the popupview to close the popup after tap.

So I'm trying to replace somehow the UIBarButtonItem's selector with my own, in which I call the already defined actions, like this :

-(SEL)extendOriginal:(UIBarButtonItem *) uibb
{
  if (popup) popup.hidden = YES;
  [uibb.target performSelector:uibb.action];
  // return ??  do what ?? 
}

But how do I replace the original selector to call this custom method with my custom UIToolbar as its target ? Or how could I "extend" the original selector with this call ? Sorry if question is lame :)

Edit: In other words, I want 2 actions with 2 separate targets to be executed when UIBarButtonItem is tapped.

Thanks!


Solution

  • Finally, I found a way to do it, not the prettiest, but it works.

    In the custom toolbar class I created in its layout method a UITapGestureRecognizer to handle taps. I've set the cancelsTouchesInView to false and in the

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    

    method I'm returning true only for my toolbar's subviews.

    This way the original target and action of each UIBarButtonItem remains the same and the supplementary code to handle my popup is the action of the UIGestureRecognizer. Another problem was to distinguish between the tapped items on the toolbar (the tapped view in touch.view is some undocumented view, yay), eventually I did it with some BOOL iVars.

    I hope this will help somebody with the same problem.