How to detect a click event in a UIWebView

本秂侑毒 提交于 2021-02-08 03:02:00

问题


I have a UITableView that loads content into a UIWebView like this:

//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];

}

All of the remote content that gets fed to the UIWebView comes from a web service that I wrote. The first screenshot is what is presented when a word is selected from the list view titled "Glossary"

Detail view controller with UIWebView HTML content

This all is great until you follow a link, in this case clicking on the "frame" link in the UIWebView content. The new content gets loaded but I need to tell the nav bar that the UIWebView has changed so I can at least change the white title text to match the new content. What UIWebView methods should I implement and where should they go?

enter image description here


回答1:


Set your view controller to be the UIWebView's delegate, then implement

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


来源:https://stackoverflow.com/questions/9196585/how-to-detect-a-click-event-in-a-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!