2
votes

I have a UITable and a UITextView on a View Controller. I want to have a custom UIMenuController with options 'Copy' and 'Info' when I longpress the tableviewcell and I want to have the default UIMenuController on longpress of UITextView.

I searched for a solution and now I am able to create a custom UIMenuItem 'Info'. But, when I long press the UITextView I see that 'Info' is appended as well.

How can I set custom UIMenuController only for the UITableView and not for UITextView? I want the default options alone in the UITextView.

2

2 Answers

0
votes

Bit late here,
I also had same issue where the default UIMenuController default items get added to custom one as its a shared instance control across the application.
  You should subclass your UITextView and add method:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
      if ( action== @selector(yourMethodInfo:)) {
                    return NO; }

                     return YES;

    }

Also don't used sharedInstance of UIMenuController and create a separate object for your Custom TextView subclass and use that. After you invoke the UIMenuController from your UITableView you should do
  [menuController setMenuItems:nil]; [menuController setMenuVisible:NO]; [menuController update]; menuController= nil; After a menu item is selected on your selector method.


  If someone needs more reference let me know, I can provide more explanations .

0
votes

Your question:

How can I set custom UIMenuController only for the UITableView and not for UITextView? I want the default options alone in the UITextView.

The answer is: You can't.

Reason: You can only get the UIMenuController Instance by invoking class method like this:[UIMenuController sharedMenuController]. See Apple's documentation:UIMenuController

How to solve the issue:

Set your custom UIMenuItems nil When shared UIMenuController will hide.

Example code:

-(void)longPress:(UILongPressGestureRecognizer*)gesture {
    //other stuff code...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMenuWillShowNotification:) name:UIMenuControllerWillShowMenuNotification object:nil];
}

-(void)handleMenuWillShowNotification:(NSNotification*)notification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMenuWillHideNotification:) name:UIMenuControllerWillHideMenuNotification object:nil];
}

-(void)handleMenuWillHideNotification:(NSNotification*)notification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillHideMenuNotification object:nil];
    UIMenuController *menuController = notification.object;
    [menuController setMenuItems:nil];
}