0

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!

Templar
  • 1,694
  • 1
  • 14
  • 32

2 Answers2

0

This -(SEL)extendOriginal:(UIBarButtonItem *) uibb doesn't make any sense.

I assume your are setting the target and the action of the bar button item somewhere. There you can set any method with one argument id or UIBarButtonItem* as the selector.

Therefore try to change your code to

- (void)myMethod:(UIBarButtonItem *) uibb
{
  if (popup) popup.hidden = YES;

  // do cool stuff here
}

and set the target as

[[UIBarButtonItem alloc] initWithTitle: @"Blabla" style: UIBarButtonItemStylePlain target: self action: @selector(myMethod:)]; 
dasdom
  • 13,975
  • 2
  • 47
  • 58
  • Yes, I know it doesn't make sense :) I'm adding the UIBarButtonItems in a viewcontroller which has one of my custom toolbars. My problem is that I want to implement it transparently, so the other viewcontroller where the button actions are defined should have no code for this implementation detail (btw, currently it has, but that's why I wnt a custom toolbar to handle this issue automatically. – Templar Apr 12 '12 at 11:23
  • Edited my question, to be more precise. – Templar Apr 12 '12 at 11:35
0

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.

SahilS
  • 396
  • 5
  • 7
Templar
  • 1,694
  • 1
  • 14
  • 32