(Iphone/ipad Development) customizing the menu bar in UIWebView

Source: Internet
Author: User

First of all, the following method is I have been the actual operation to achieve the expected effect after the summary, you can take a few detours in reference, rest assured to try.

We want to achieve the desired effect is to load the Web page with UIWebView, long press a word and pop up our custom menu bar, do not show the system itself, click on a menu to handle the corresponding.

First, customize the desired menu bar first

Uimenucontroller *menucontroller = [Uimenucontroller Sharedmenucontroller];
UIMenuItem *menuitemciyi = [[UIMenuItem alloc] initwithtitle:@ "See meaning" Action: @selector (Ciyi:)];
UIMenuItem *menuitemsound = [[UIMenuItem alloc] initwithtitle:@ "Listen to pronunciation" Action: @selector (Listeningsound:)];
UIMenuItem *MENUITEMSHENGCI = [[UIMenuItem alloc] initwithtitle:@ "Add new words to this" action: @selector (Addword:)];
Nsarray *marray = [Nsarray arraywithobjects:menuitemciyi,menuitemsound,menuitemshengci, Nil];
[Menuitemciyi release];
[Menuitemsound release];
[MENUITEMSHENGCI release];
[Menucontroller Setmenuitems:marray];

As you can see, each menu corresponds to the name of the action method, as to where this code is written, and later, this is where additional attention is needed.

Customize an inherited UIWebView control Customwebview, manually handle the action of a menu

In the CUSTOMWEBVIEW.M

//
customwebview.m
Lrcwebviewtest
//
Created by Andy on 12-6-20.
Copyright (c) 2012 __mycompanyname__. All rights reserved.
//

#import "CustomWebView.h"

@implementation Customwebview

-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Initialization code

}
return self;
}
-(void) awakefromnib{

}
-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
if (action = = @selector (ciyi:) | | action = = @selector (listeningsound:) | | Action = = @selector (addword:)) {
return YES;
}
return NO;
}

-(Ibaction) Ciyi: (ID) sender; {
NSLog (@ "Ciyi");
}

-(Ibaction) Listeningsound: (ID) sender{
NSLog (@ "Listeningsound");
}

-(Ibaction) Addword: (ID) sender{
NSLog (@ "Addword");
}


/*
Only override Drawrect:if perform custom drawing.
An empty implementation adversely affects performance during animation.
-(void) DrawRect: (cgrect) rect
{
Drawing Code
}
*/

@end

By rewriting-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender function, tell the menu bar which menus can be displayed.

Then there is the discussion of the custom menu bar, where it should be written.

Before is written in Customwebview.m's initWithFrame method, but the long press after the menu bar has not come out, later found that if you add this control through Xib, through the breakpoint tracking can be found that it will not execute the initWithFrame method, so the code should be written in- (void) In the Awakefromnib method, if it is added through code, it should be written in the initWithFrame method, otherwise the action of adding a menu is not performed.

Also, just in the process of trying to find that if you add a menu bar code in the Add UIWebView control Interface Viewdidload can also achieve the corresponding effect, perhaps you will ask, this is to change the interface of the menu bar, Will the menu bar that pops up in that interface look the same? The answer is no, because by rewriting

-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) Sender This method can filter out the required menu items, and the custom WebView has rewritten this method, so the WebView is shot Out of the menu is required menu items, in other controls such as the TextField control, the pop-up will still be the system default menu items, only for program readability, maintainability considerations, it is best to modify the menu in the custom control, individually encapsulated together.

(Iphone/ipad Development) customizing the menu bar in UIWebView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.