Gaby Fitcal
Gaby Fitcal

Reputation: 1944

Add a UIMenuItem for a specific UITextView?

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

Upvotes: 2

Views: 609

Answers (2)

Vizllx
Vizllx

Reputation: 9246

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]];
       }];
     }
 }

Upvotes: 1

user5250945
user5250945

Reputation:

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.

Upvotes: 1

Related Questions