【问题标题】:button in webView execute Objective C methodwebView中的按钮执行Objective C方法
【发布时间】:2015-03-09 14:56:05
【问题描述】:

UIWebview中有一个按钮,我需要实现点击这个按钮时跳转另一个UIView的功能,也就是说,执行以下功能:[self performSegueWithIdentifier:@"homePage" sender:self];

我尝试使用 shouldStartLoadWithRequest 函数,但它不起作用。如果您能给我看一个小演示,您将不胜感激!谢谢。

【问题讨论】:

    标签: javascript ios webview


    【解决方案1】:
    <!--index.html-->
    <p>Hello World!</p>
    <p><h1>Link</h1>
    <a href='appintern://home'>Home</a>
    </p>
    <p>
    <h1>Button</h1>
    <form method="post" action="home">
        <button type="submit">Continue</button>
    </form>
    </p>
    

    将 html 加载到 web 视图中:

    // html path
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    // html data
    NSData *htmlData = [NSData dataWithContentsOfFile:htmlPath];
    // load html in webview
    [self.mainWebView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];
    

    在委托方法中:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
        if (navigationType == UIWebViewNavigationTypeFormSubmitted) {
            NSString *destination = [request.URL lastPathComponent];
            if ([destination isEqualToString:@"home"]) {
                NSLog(@"go to home");
                //[self performSegueWithIdentifier:@"homePage" sender:self];
                return NO;
            }
        }
        else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            // todo
        }
    
        return YES;
    }
    

    希望对你有帮助。

    【讨论】:

    • 不错!我忘了设置 webview 的委托。问题已解决。感谢您的帮助。
    【解决方案2】:

    您需要使用 URL 方案从您的 html 网页与 Obj C 进行通信:http://www.macstories.net/tutorials/guide-url-scheme-ios-drafts/

    让您的提交按钮打开您将获得所需结果的 URL 方案。提示:URL 方案实际上是类似于 API 的调用。您要确保它们包含足够的识别信息,以便可以智能地添加更多方案。

    【讨论】:

    • 感谢您的回复。这也很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多