custom buttons on uiwebview(iphone)

社会主义新天地 提交于 2019-12-04 17:18:28

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

Something like:

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

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.

Execute custom code by clicking on a link(button) in html

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

Then implement shouldStartLoadWithRequest.

You button links should look like this:

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

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

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

That's it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!