Sagar,
Your question is a couple of months old, but I finally figured this one out, so I figured I'd answer it in case it helps out someone else.
I added the following code to the viewDidAppear: method of the view controller that contains the webview.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIMenuItem *customMenuItem1 = [[[UIMenuItem alloc] initWithTitle:@"Custom 1" action:@selector(customAction1:)] autorelease];
UIMenuItem *customMenuItem2 = [[[UIMenuItem alloc] initWithTitle:@"Custom 2" action:@selector(customAction2:)] autorelease];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]];
}
In my viewDidDisappear:, I go ahead and remove those items:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[UIMenuController sharedMenuController] setMenuItems:nil];
}
Then, I implemented the canPerformAction:withSender: method in the view controller. It helps to understand the concept of responders and responder chains to understand what is going on here. Basically, your uiviewcontroller is part of the responder chain, so it gets asked if it can handle any actions (like your custom actions you added above) that objects higher up the responder chain (like the UIWebView) don't know how to handle (see the UIResponder documentation and the Event Handling Guide for iOS for the gory details).
Now, when canPerformAction:withSender: is called for the webview, the sender parameter is set to nil. So, I try to be a bit clever about how I write this function. Basically, I make sure that the sender is nil, I'm showing the webview to the user, and any other controls on the page aren't the first responder. If that's the case, then I check to see if this is one of the actions I defined above and retur YES if it is. In all other cases I return the default value from UIViewController by calling the same method on super.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (webView.superview != nil && ![urlTextField isFirstResponder]) {
if (action == @selector(customAction1:) || action == @selector(customAction2:)) {
return YES;
}
}
return [super canPerformAction:action withSender:sender];
}
Of course, now the next step is figuring out how to actually do something with the selection (probably by running some JavaScript in the webview).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…