2

I have a lot of UITextViews in my screen and i want to add a new custom UIMenuItem for one of them.

It is possible to add a custom UIMenuItem just for a specific UITextView?

I use below code but i have my custom button for all UITextViews

UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Read" action:@selector(readButtonAction)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];

Thanks

Gaby Fitcal
  • 1,814
  • 1
  • 17
  • 28

2 Answers2

1

I did not test it, but you could reset the menuItems when one of the UITextView become first responder. I'd do that by subclassing UITextView and implementing the becomeFirstResponder.

1

If you want UIMenuItem to be displayed for a specific UITextView you can just add menuItem in textFieldDidBeginEditingmethod and add a specific tag to the UITextView so that custom menu item displayed for that only :-

-(void)textViewDidBeginEditing:(UITextView *)textView

{
  if(textview.tag==999)  //specify for which textveiw you want custom menu 
   {
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{
   UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Read" action:@selector(readButtonAction)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
       }];
     }
 }
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • Yeah, but @Charles Thierry have right. You need to reset every time when is not my specific UITextView. Just add a else there and setMenuIetems with empty array and will working. Thank your for your answer – Gaby Fitcal Jan 27 '16 at 09:44
  • Yes, I thought else part will be handled by you ;) – Vizllx Jan 27 '16 at 09:52