打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
showing custom menu on selection in UIWebView in iphone

I want to show 2 options like "hi" & "bye" when user completes selection on UIWebView.

I have added observer to my view controller as follows. But I don't know further implementation. Please guide me.

[[UIMenuController sharedMenuController] addObserver:self                                           forKeyPath:UIMenuControllerWillShowMenuNotification                                             options:nil                                             context:nil ];

Thanks in advance for sharing your great knowledge.

Sagar

asked Jun 2 '10 at 6:05
Sagar R. Kothari
11.7k42131213

up vote 42 down vote accepted

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).

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
gif图片保存和UIwebView显示代码实例 | 开发资源分享区
长按UITableViewCell弹出UIMenuController | LvesLi's Blogging
在UIWebView中自定义显示选中文字的编辑菜单项 | 码癖
ios中播放gif动画 | iOS开发讨论区
UIWebView 前进 后退 刷新 取消
WKWebView特性及使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服