uiwebview上的自定义按钮(iphone)
我想在webView上添加自定义按钮。 当我在url上尝试任何东西时,他们也应该在那里。
这怎么可能??
基本上我想把按钮放在uiwebView上,它们是自定义按钮
//编辑的代码……
我这样做…这里链接出现但方法没有被调用…并且代码中没有任何错误.. 🙂
NSString *imagePath = [[NSBundle mainBundle] resourcePath]; imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"]; imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; NSString *HTMLData = @"<htmlClick me!-->
"; [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; }
你只需要使用链接并设置它们的样式。
就像是:
Click me!
看看http://www.cssbuttongenerator.com/ ,很容易创建自己的按钮,让它为你生成css代码。 你真的必须点击按钮创建自己来生成代码。
通过单击html中的链接(按钮)来执行自定义代码
首先,您必须遵守UIWebViewDelegate协议,并相应地设置委托。
然后实现shouldStartLoadWithRequest
。
您的按钮链接应如下所示:
Click me!
我们正在使用我们组成的自定义协议: 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; }
而已。