Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass variable from UIViewController to delegate UITableViewController
    primarykey
    data
    text
    <p>I'm relatively new to iOS programming, so bear with me here. The requirement I'm working with is as follows:</p> <blockquote> <p><strong>Screen 1:</strong> User is shown a UITableView loaded with data, specifically City and State. The user taps on a selection and is taken to Screen 2.</p> <p><strong>Screen 2:</strong> The user is shown a "detailed" view of the item they selected for editing.</p> <ul> <li>The City string (<strong><em>selectedCity</em></strong>) is placed in a Text Box for editing</li> <li>An NSArray of States is loaded in a UITableView. The state for the selected City should be selected in the table on load based on the string passed from Screen 1 (<strong><em>selectedState</em></strong>). The user can select a different State here if desired.</li> <li>A 2nd UITableView will contain an array of Schools related to the city and retrieved from the database. There is a separate set of functionality for managing this list.</li> </ul> </blockquote> <p>I have not had any issues implementing Screen 1. Here's how I passed the city and state values through to screen 2:</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Get the City self.checkedCellIndex = indexPath.row; FSCity *selectedCity = [CityList objectAtIndex:indexPath.row]; NSString *cityName = selectedCity.name; NSString *stateName = selectedCity.state; AdminCityView *dvController = [[AdminCityView alloc] initWithNibName:@"AdminCityView" bundle:[NSBundle mainBundle]]; dvController.selectedCity = cityName; dvController.selectedState = stateName; [self.navigationController pushViewController:dvController animated:YES]; dvController = nil; } </code></pre> <p>In order to build Screen 2, however, I had to create UITableViewControllers for each UITableView (StateTable and SchoolTable) and load them in to the main UIViewController (AdminCityView), like so:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; txtCity.text = selectedCity; if (stateController == nil) { stateController = [[StateTable alloc] init]; } if (schoolController == nil) { schoolController = [[SchoolTable alloc] init]; } [tblState setDataSource:stateController]; [tblSchool setDataSource:schoolController]; [tblState setDelegate:stateController]; [tblSchool setDelegate:schoolController]; stateController.view = stateController.tableView; schoolController.view = schoolController.tableView; } </code></pre> <p>While I can get at the selectedState variable just fine in my UIViewController and my State table loads just fine in the UIView, I'm stuck at how to pass the selectedState variable from my UIViewController (AdminCityView) to the UITableViewController (StateTable) so that I can perform operations on StateTable with the variable (such as selecting the row that matches the variable).</p> <p>I've been testing with NSLog on both AdminCityView and StateTable, and the variable is definitely only accessible from AdminCityView currently. I'm not sure if I need to pass it via code in AdminCityView, or if I should just go back to Screen 1 and somehow pass it to StateTable prior to navigating to AdminCityView.</p> <p>I'm happy to post any additional code that might be helpful toward reaching an answer. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload