Search code examples
iphoneiosuimenucontroller

single button UIMenuController


Good morning !

I'm writing an app which displays an outlet list, every outlet can sell 2 kind of product and / or can be an online shop.

For show this on my app I have added some "icons", but in case of they are bot enough "self explanatory" I'm looking for an easy-to-use way to explain.

So right know I'm using an UImenuController when the user tap on the icon (which is an UIButton)

I'm just looking for something simple, like the picture below :

enter image description here

But instead of the

Highlight

I just want to put something like

Sell x product

The problem I'm facing is that I can display this UIMenuController, but he has many many "garbages values" as you can see in this picture :

enter image description here

Not really cool right?

He is the method linked to my button :

-(void)buttonClicked:(UIBarButtonItem*)sender event:(UIEvent*)event{

    [self becomeFirstResponder];

    UIView *buttonView=[[event.allTouches anyObject] view];
    CGRect buttonFrame=[buttonView convertRect:buttonView.frame toView:self.view];

    UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Select" action:@selector(selectItem:)];

    UIMenuController *menu = [UIMenuController sharedMenuController];

    [menu setMenuItems:[NSArray arrayWithObjects:flag, nil]];

    [menu setTargetRect:buttonFrame inView:self.view];

    [menu setMenuVisible:YES animated:YES];
}

I have obviously :

✔ Returned YES for -(BOOL)canBecomeFirstResponder

✔ Returned YES for -(BOOL)canPerformAction

✔ Returned YES for -(BOOL)canPerformAction

✔ Returned YES for -(BOOL)canPerformAction -(BOOL)canPerformAction:(SEL)action withSender:(id)sender

✔ The text "Select" does appear, but at the "end" of the menu (you must use the arrows to the right)

Any help and / or clue are welcome ! :-)


Solution

  • Well, I finally managed (after spending the afternoon on Google & Stackoverflow) to achieve it !

    It's easy : when you overwrite

    -(BOOL)canPerformAction -(BOOL)canPerformAction:(SEL)action
    

    Just return

    [super canPerformAction:action withSender:sender];
    

    instead of

    YES
    

    and "voilà !"

    :)