0
votes

When i touch a label UIMenuController appears with my custom list of items, and that works well. But when UIMenuController appear in a standard UISearchBar i see all my custom items there too. Why?

I need to show only standard (copy, paste) items for standard UISearchBar and custom items when i touch the label. Can you explain how should i do that?

UPDATE

What i did (not good solution):

If we have keyboard, that is searchbar, if we don't, that is label.

Flag, which mean which items list i will use

BOOL standardList;

Register for keyboard appear/disappear

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

    [super viewWillDisappear:animated];
}

-(void)keyboardWillShow:(id)sender
{
    standardList = YES;
}

-(void)keyboardWillHide:(id)sender
{
    standardList = NO;
}

And check flag, in next method:

- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender
{

    if (selector == @selector(copy:))
    {
        return YES;
    }

    if (!standardList)
    {
        if ((selector == @selector(makeCall:)) || (selector == @selector(createNewContact:)))
        {
            return YES;
        }
    }

    return NO;
}

That work well, BUT: iPad keyboard have "hide keyboard" button, and keyboard can hide without [UISearchBar resingFirstResponder].

Even if i add next lines:

-(void)keyboardWillHide:(id)sender
{
    standardList = NO;
    [mySearchController setActive:NO];
}

that's still bad solution - i can't hide keyboard while searching and scroll search results.

Any suggestions?

1

1 Answers

0
votes

One solution: u can always reset the UIMenuItems after u customize UIMenuController. addObserver of UIMenuControllerWillHideMenuNotification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHideEditMenu:) name:UIMenuControllerWillHideMenuNotification object:nil];

then in the willHideEditMenu: function

-(void)willHideEditMenu:(NSNotification *)object
{
    //any other thing u want to do.
    [[UIMenuController shareMenuController] setMenuItems:nil]; 
}

this way, UIMenuController singleton in other places won't get affected by what u have done where u customize it.

Other solution: i think the root reason why u have this problem is u have some function with the same name of "makeCall:" or "createNewContact:" in other view or viewcontroller (basically may in any UIResponder) that is the parent view (viewcontroler) of the place where u have this problem. so check out the responder tree, see if u can find any selectors with the same name of the UIMenuItem selectors. since

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender;

is a function of any UIResponder, it could get called. and if it does happen to have a selector with the same name, it could return YES, and u will have this problem. To solve the problem, just rename the selector of the UIMenuItem.


My English isn't good. Hope u get my point and try it.