在 iphone 的 UIWebView 中显示自定义菜单

showing custom menu on selection in UIWebView in iphone(在 iphone 的 UIWebView 中显示自定义菜单)

本文介绍了在 iphone 的 UIWebView 中显示自定义菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示 2 个选项,例如hi"和当用户在 UIWebView 上完成选择时再见".

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.

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

推荐答案

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.

我将以下代码添加到包含 webview 的视图控制器的 viewDidAppear: 方法中.

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]];
}

在我看来DidDisappear:,我继续删除这些项目:

In my viewDidDisappear:, I go ahead and remove those items:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [[UIMenuController sharedMenuController] setMenuItems:nil];
}

然后,我在视图控制器中实现了 canPerformAction:withSender: 方法.它有助于理解响应者和响应者链的概念,以了解这里发生了什么.基本上,您的 uiviewcontroller 是响应者链的一部分,因此它会被询问是否可以处理响应者链上方的对象(如 UIWebView)不知道如何处理的任何操作(例如您在上面添加的自定义操作)(请参阅 UIResponder 文档 和 iOS 事件处理指南 了解血淋淋的细节).

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

现在,当为 webview 调用 canPerformAction:withSender: 时,sender 参数设置为 nil.所以,我试着对我如何编写这个函数有点聪明.基本上,我确保发送者为零,我向用户显示 web 视图,并且页面上的任何其他控件都不是第一响应者.如果是这种情况,那么我会检查这是否是我在上面定义的操作之一,如果是,则返回 YES.在所有其他情况下,我通过在 super 上调用相同的方法从 UIViewController 返回默认值.

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];
}

当然,现在下一步是弄清楚如何对选择进行实际操作(可能通过在 web 视图中运行一些 JavaScript).

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

这篇关于在 iphone 的 UIWebView 中显示自定义菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iphone 的 UIWebView 中显示自定义菜单

基础教程推荐