如何使本机iOS应用程序响应UIWebView中的点击事件?
问题描述:
说我可能有一个UIWebView
显示一个页面,其中包含一个link
。当用户点击链接时,是否可以触发一个事件来更新本机UI
? 例如,当用户单击链接时,我想要更改UIProgressView
中的进度值。有没有办法做到这一点?谢谢!如何使本机iOS应用程序响应UIWebView中的点击事件?
答
您需要实现webView:shouldStartLoadWithRequest:navigationType:protocol方法。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// the request variable contains the request the user clicked on.
}
答
尝试使用这种方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return TRUE;
}
,你可以检查navigationType值:
enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
};
答
您可以在下面
用在UIWebView中检测到触摸事件的委托方法- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(@"Touches began");
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(@"Touches moved");
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(@"Touches ended");
}
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(@"Touches cancelled");
}
您必须添加UIWebView覆盖主视图并添加一个扩展UIWindow来捕获触摸事件的自定义类。
你可以找到一步一步的教程博客here。
什么是'WebUIView'你的意思是'UIWebView'? – Popeye
http://stackoverflow.com/questions/15537320/invoke-method-in-objective-c-code-from-html-code-using-uiwebview/15541607#15541607看看这个问题 – Vinodh