uiwebview上的自定义按钮(iphone)

时间:2011-05-07 09:00:50

标签: iphone objective-c ios uiwebview

我想在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]]];

然后

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

1 个答案:

答案 0 :(得分:8)

您只需使用链接并设置样式即可。

类似的东西:

<a href="#" class="buttonStyle">Click me!</a>

看看http://www.cssbuttongenerator.com/,很容易创建自己的按钮并让它为您生成css代码。您实际上必须单击按钮创建自己以生成代码。

点击html

中的链接(按钮)执行自定义代码

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

然后实施shouldStartLoadWithRequest

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

<a href="button://dosomething" class="buttonStyle">Click me!</a>

我们使用的是自定义协议: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;
}

就是这样。