uiwebview上的自定义按钮(iphone) [英] custom buttons on uiwebview(iphone)

查看:114
本文介绍了uiwebview上的自定义按钮(iphone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add custom button on webView. they should also should be there when i try anything in url.

怎么可能?

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

basically i want to put buttons on uiwebView and they are custom buttons

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

//edited code...

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

I am doing this...here link is appearing but method is not getting called...and there wasnot any error in your code ..:)

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


推荐答案

你只需要使用链接和样式。

You'll just have to use links and style them.

类似于:

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

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

Have a look at http://www.cssbuttongenerator.com/, very easy to create your own button and let it generate the css code for you. You'll actualy have to click on the button create itself to generate the code.

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

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

First of all you've got to conform to the UIWebViewDelegate protocol, and set the delegate accordingly.

然后实现 shouldStartLoadWithRequest

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

You button links should look like this:

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

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

We are using a custom protocol we make up: button://.

现在实现这样的shouldStartLoadWithRequest:

Now implement shouldStartLoadWithRequest like this:

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

就是这样。

这篇关于uiwebview上的自定义按钮(iphone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆