Question

I have my storyboard set up like this:

  1. A view defined as a custom class, let's call it V1ViewController
  2. This view has a view container
  3. The view container has a UITableViewController with a set of static cells
  4. The cells are of style "Left Detail"

I'm trying to access the text box in the Left Detail from the code in V1ViewController, but can't seem to figure out how to traverse that hierarchy. Any help would be appreciated.

Was it helpful?

Solution

This is the proper way to get a reference to your child view controller:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([segue.identifier isEqualToString: @"childViewControllerSegue"]) {
       ChildViewController *myChildViewController = (ChildViewController *) [segue destinationViewController];
       //With myChildViewController you can access your child view controller for example:
       [myChildViewController.myTableView reloadData];   
   }
}

Keep in mind that because this is a Container view this method will be called upon load of your ViewController. Therefore it would be a good idea to save myChildViewController as a property or an instance variable for later use.

OTHER TIPS

I am going to disagree with most of the other responders. I strongly advise against trying to manipulate the child view controller's table view (or any of it's view contents.)

You should treat a view controller's view hierarchy as private.

Instead, I would save a pointer to the child view controller in your prepareForSegue method (as shown in sha's post).

Make your child view controller a custom subclass of UITableViewController with methods that let you specify new content for the left views of your static cells.

Then, in the parent view controller, when you want to change something in the table view, use the saved pointer to the custom table view controller (myChildViewController) to invoke methods that make the desired changes for you. It's pretty easy.

I have a sample app on github that illustrates using container views and static table views. I wanted my design to be as general-purpose as possible, so I defined protocols for both child-to-parent and parent-to-child communications.

Check out this demo of using static table views in a container view.

Move in trash your's containter view with tableviewcontroller. Use instead UITableView. You can easily create IBOutlet on it. Or you can even subclass yours V1ViewController from UITableViewController (depends on what should be displayed on the screen). And then implement in yours V1ViewController UITableViewDelegate and UITableViewDataSource required methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top