通过单击UIWebView中的按钮调用方法 [英] Call a method by clicking a button in a UIWebView
问题描述
我想生成一些HTML内容,并将其放入UIWebView。 HTML包含一些按钮。是否可以定义这些按钮的操作?我想在有人在UIWebView中按下此按钮时调用我的目标代码中的一个方法(例如firstButtonPressed)。
感谢您帮助我。
只要跟进这一点,就可以,而不是难以将HTML / javascript事件连接到objective-c方法。 p>
Damien Berrigaud提出了一个 $
$<$ p $ p>
//以文件开头的地图链接://
//以#action
结尾//使用控制器的操作(如果存在)。
//
//在Safari中打开其他链接。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString * fragment,* scheme;
if(navigationType == UIWebViewNavigationTypeLinkClicked){
[webView stopLoading];
fragment = [[request URL] fragment];
scheme = [[request URL] scheme];
if([scheme isEqualToString:@file]&& [self responsesToSelector:NSSelectorFromString(fragment)]){
[self performSelector:NSSelectorFromString(fragment)
return NO;
}
[[UIApplication sharedApplication] openURL:[request URL]];
}
return YES;
}
I want to generate some HTML content and put this into a UIWebView. The HTML contains some buttons. Is it possible to define actions for these buttons? I want to call a method (e.g. firstButtonPressed) in my objective-c code when someone presses this button in the UIWebView.
Thanks you for helping me.
Just to follow up on this, it is possible, and not to difficult to connect HTML/javascript events to objective-c methods.
Damien Berrigaud came up with a good example of how to do that by using file://
links
// Map links starting with file://
// ending with #action
// with the action of the controller if it exists.
//
// Open other links in Safari.
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType {
NSString *fragment, *scheme;
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[webView stopLoading];
fragment = [[request URL] fragment];
scheme = [[request URL] scheme];
if ([scheme isEqualToString: @"file"] && [self respondsToSelector: NSSelectorFromString(fragment)]) {
[self performSelector: NSSelectorFromString(fragment)];
return NO;
}
[[UIApplication sharedApplication] openURL: [request URL]];
}
return YES;
}
这篇关于通过单击UIWebView中的按钮调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!