IOS 7 - Advanced UIWebView Menu

I want to add an extra menu item to the menu that appears when any text is selected.

I added the code below viewDidLoad:

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

[UIMenuController sharedMenuController].menuItems = extraItems;

I have also rewritten my UIWebView user interface in the following ways:

- (void)bold:(id)sender {

}

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

Therefore, sometimes, when I select text, the menu pacifies, but sometimes it is not. I do not know what the problem is.

+3
source share
1 answer

Your extraItems array should have NSArray, not 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 the extraItems array to sharedMenuController just like you do.

0
source

All Articles