3

这似乎很奇怪。也不知道有没有可能!!

我有一个加载本地 html 页面的 UIWebView。在这个 html 页面上,我有一个按钮。

我想单击按钮,然后在 Xcode 中调用 IBAction。

我该怎么办?或者你甚至可以这样做???

多谢你们,

斯特凡。

4

3 回答 3

8

您可以使用自定义协议来做到这一点。在您的 html 文件中,您可以链接到类似myProtocol://callSomeAction.

然后在您的UIWebViewDelegate(可能是您的UIViewController)上,您必须实现名为:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

(这里的文档)

这个想法是,在该代码上,您可以根据request参数中的数据检测协议。如果是myProtocol,您可以致电您的IBAction并返回NO。如果是别的东西,你回退到让 web 视图加载页面,然后返回YES.

代码看起来像这样:

- (BOOL)webView:(UIWebView *)webView
    shouldStartLoadWithRequest:(NSURLRequest *)request 
    navigationType:(UIWebViewNavigationType)navigationType {

    NSString* scheme = [[request URL] scheme];
    if ([@"myProtocol" isEqual:scheme]) {
        // Call your method
        return NO;
    } else {
        return YES;
    }
}
于 2010-11-08T12:48:21.113 回答
3
  1. 让 HTML 页面上的按钮打开一个自定义 URL,例如myapp://buttonclick.

  2. 在您的 Web 视图委托中,实现webView:shouldStartLoadWithRequest:navigationType:. 检查请求是否包含您的自定义 URL,如果包含,请调用您想要的任何 Obj-C 方法。

于 2010-11-08T12:48:10.413 回答
0

我有类似的情况,但它是一个 imageclick , href 和处理这种 webview 方法,

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener{

    NSString *host = [[request URL] host];
    //if (host!=nil) 
    {




    WebNavigationType eActionType = (WebNavigationType)[[actionInformation valueForKey:WebActionNavigationTypeKey] intValue];
    NSURL *pOrignalURL;
    if(eActionType == WebNavigationTypeLinkClicked)// == [actionInformation valueForKey:WebActionNavigationTypeKey])
    {
        /* we will handle it */
        pOrignalURL = [actionInformation valueForKey:WebActionOriginalURLKey];
        NSString *pElementName = [actionInformation valueForKey:WebActionElementKey];

        if([[pOrignalURL absoluteString] hasPrefix:@"app:"]){

            [listener ignore];
            return;
        }
    }
    //[[NSWorkspace sharedWorkspace] openURL:pOrignalURL];
    NSArray* urls = [ NSArray arrayWithObject:
                     [ NSURL URLWithString:[pOrignalURL absoluteString]]];

    [[ NSWorkspace sharedWorkspace ]
     openURLs:urls
     withAppBundleIdentifier:nil
     /* use default system bindings */
     options:NSWorkspaceLaunchWithoutActivation
     additionalEventParamDescriptor:nil
     launchIdentifiers:nil ];


     /* default behavior */
[listener download];




    return;
    }
}
于 2011-11-11T14:42:29.920 回答