3

I want to add additional menu item for the menu that appears when some text is selected.

I have added the code below to viewDidLoad:

NSMutableArray *extraItems = [[NSMutableArray alloc] init];
UIMenuItem *boldItem = [[UIMenuItem alloc] initWithTitle:@"Bold"
                                                      action:@selector(bold:)];
[extraItems addObject:boldItem];

[UIMenuController sharedMenuController].menuItems = extraItems;

I also have overwritten my custom UIWebView with these methods:

- (void)bold:(id)sender {

}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(bold:))
        return YES;
    return [super canPerformAction:action
                        withSender:sender];
}

So sometimes when I highlight the text the menu appeasers, but sometimes it does not. I don't know what the problem is.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

1 Answers1

0

Your array "extraItems" should be an NSArray, not an NSMutableArray, so your first three lines that you added to viewDidLoad should be replaced by:

UIMenuItem* extraItem = [[UIMenuItem alloc] initWithTitle:@"Bold" action:@selector(bold:)]; NSArray* extraItems = [NSArray arrayWithObject:extraItem];

Then add this version of extraItems array to the sharedMenuController just as you did.

StayCalm
  • 1
  • 1