uiwebview上的自定义按钮(iphone)

Jea*_*ard 2 iphone objective-c uiwebview ios

我想在webView上添加自定义按钮.当我在网址上尝试任何东西时,他们也应该在那里.

这怎么可能??

基本上我想把按钮放在uiwebView上,它们是自定义按钮

//编辑的代码......

我这样做...这里链接出现但方法没有被调用...并且代码中没有任何错误.. :)

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"<html<a href=\"button://dosomething\" class=\"buttonStyle\">Click me!</a>--></style><br><br>";

[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];
Run Code Online (Sandbox Code Playgroud)

然后

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request   navigationType:(UIWebViewNavigationType)navigationType 
{
    // only do something if a link has been clicked...
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {     

        // check if the url requests starts with our custom protocol:
        if ([[[request URL] absoluteString] hasPrefix:@"button://"]) {
            // Do custom code
            return NO;
        } 
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 8

你只需要使用链接并设置它们的样式.

就像是:

<a href="#" class="buttonStyle">Click me!</a>
Run Code Online (Sandbox Code Playgroud)

看看http://www.cssbuttongenerator.com/,很容易创建自己的按钮,让它为你生成css代码.你真的必须点击按钮创建自己来生成代码.

通过单击html中的链接(按钮)来执行自定义代码

首先,您必须遵守UIWebViewDelegate协议,并相应地设置委托.

然后实施shouldStartLoadWithRequest.

你的按钮链接应如下所示:

<a href="button://dosomething" class="buttonStyle">Click me!</a>
Run Code Online (Sandbox Code Playgroud)

我们正在使用我们组成的自定义协议:button://.

现在实现这样的shouldStartLoadWithRequest:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
    // only do something if a link has been clicked...
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {     

            // check if the url requests starts with our custom protocol:
        if ([[[request URL] absoluteString] hasPrefix:@"button://"]) {
            // Do custom code
            return NO;
        } 
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

而已.