2013-07-02 21 views
0

說我可能有一個UIWebView顯示一個頁面,其中包含一個link。當用戶點擊鏈接時,是否可以觸發一個事件來更新本機UI? 例如,當用戶單擊鏈接時,我想要更改UIProgressView中的進度值。有沒有辦法做到這一點?謝謝!如何使本機iOS應用程序響應UIWebView中的點擊事件?

+1

什麼是'WebUIView'你的意思是'UIWebView'? – Popeye

+0

http://stackoverflow.com/questions/15537320/invoke-method-in-objective-c-code-from-html-code-using-uiwebview/15541607#15541607看看這個問題 – Vinodh

回答

0

您需要實現webView:shouldStartLoadWithRequest:navigationType:protocol方法。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    // the request variable contains the request the user clicked on. 
} 
2

嘗試使用這種方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    return TRUE; 
} 

,你可以檢查navigationType值:

enum { 
    UIWebViewNavigationTypeLinkClicked, 
    UIWebViewNavigationTypeFormSubmitted, 
    UIWebViewNavigationTypeBackForward, 
    UIWebViewNavigationTypeReload, 
    UIWebViewNavigationTypeFormResubmitted, 
    UIWebViewNavigationTypeOther 
}; 
0

您可以在下面

用在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

相關問題