2012-02-08 48 views
2

我有一個加載內容到這樣一個UIWebView一個UITableView:如何在一個UIWebView檢測到點擊事件

//MainViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ]; 
     UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:bounds]; 
     UIWebView *htmlView = [ [ UIWebView alloc ] initWithFrame:[scrollView bounds]]; 
htmlView.frame = CGRectMake(10,10,280,360); 
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //Load the request in the UIWebView. 
    [htmlView loadRequest:requestObj]; 

    scrollView.contentSize = [htmlView bounds].size; 
    [scrollView addSubview:htmlView];  
    [detailsViewController.view addSubview:htmlView]; 


    [htmlView release]; 
    htmlView = nil; 

    [scrollView release]; 
    scrollView = nil; 


    [[self navigationController] pushViewController:detailsViewController animated:YES]; 
    [detailsViewController release]; 

} 

所有這一切被送入UIWebView的遠程內容都來自一個Web服務,我中寫道。第一張截圖是當從標題爲「詞彙表」列表視圖中選擇一個字呈現什麼

Detail view controller with UIWebView HTML content

這一切是偉大的,直到你點擊一個鏈接,在這種情況下,點擊在「幀」鏈接UIWebView內容。新的內容被加載,但我需要告訴導航欄UIWebView已更改,所以我至少可以更改白色標題文本以匹配新內容。我應該實現哪些UIWebView方法,它們應該放在哪裏?

enter image description here

+0

http://stackoverflow.com/questions/3201264/open-link-from-uiwebview-into-safari -iphone/3201364#3201364 – Nyx0uf 2012-02-08 15:46:55

+0

謝謝!我按照您發佈的鏈接實施了該方法。該方法在事件發生時自動調用,還是需要顯式調用它? – Slinky 2012-02-08 16:01:56

+0

如果將視圖控制器設置爲webview委託,它將被webview自動調用。 – 2012-02-08 16:11:11

回答

3

設置你的視圖控制器是一個UIWebView的委託,然後實現

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
+0

嗯。認爲會做到這一點。在我的視圖控制器的頭文件中添加了UIWebViewDelegate:@interface MainViewController:UITableViewController {...} 並實現了.m文件中的方法,但點擊一個鏈接後,執行沒有進入實現這些方法的斷點證明了這些方法永遠不會被擊中.... – Slinky 2012-02-08 16:36:07