简体   繁体   中英

How to tell which table cell was clicked from another View Controller? iOS 7

I can't seem to find much on the net that is specific to my question.

Here's my situation; I have a Table View Controller with 5 cells that act purely as buttons that correspond to particular web sites, and each button will push to a View Controller with a web view on it, displaying the corresponding website.

Instead of having 5 different view controllers all with their own web view on it, is there some way to programatically determine which table cell was clicked so that I can change the URL in which the Web View references? That way I only need one View Controller and one Web View, and depending on the particular cell that was selected it will display a different website in that Web View. How might I do this? I just feel like it is redundant to have 5 very similar view controllers.

See below for my current code and some screen shots.

This code simply loads the URL into the web View (Websites.m)

NSURL *cpaURL = [NSURL URLWithString:@"http://www.cpagroup.com.au"];  // Define new URL variable
NSURLRequest *myRequest = [NSURLRequest requestWithURL:cpaURL];  // Define new web page request
[cpaWebView loadRequest:myRequest];  // Load request to page
cpaWebView.scalesPageToFit = YES;  // Scale page to fit on the screen

Link between table and one web view

表格和Web视图链接

Just pass the NSURL string into the webSite file ( Check here for passing data ), In your didSelectRow .

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  // initialise your website class file
   websiteObj.urlString = // Use the Array OR something which you had reference of cell data
}

And in your website class, use the urlString to load the webView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM