n00bAppDev n00bAppDev - 1 year ago 71
iOS Question

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

Table and Web View Link

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download