12
votes

I am new to iOS App Development. I have connected a tableview controller such that when I select one of the rows I get another UIViewController using didSelectRowAtIndexPath. I have a container view inside this UIViewController which displays (say for the time being) index of the row on which didSelectRowAtIndexPath was called. I want to do this using a segue, but the problem is I don't know how to get a reference to the view controller which is formed by using the container view. I know you can get the destination View Controller using segue.destinationViewController in the prepareForSegue but how do I get a reference to view controller that will be loaded because of the container view. I am building the app for iOS 6. Also I have used Storyboard for the UIs. Thanks

Edit:

This question basically comes down to how to get a reference to the UIViewController-2 that is being pointed by the UIContainerView which is inside the UIViewController-1. The UIViewController-1 is triggered by selecting a row of UITableViewController

UITableViewController (selecting a row to give)---> UIViewController-1 which contains....ContainerView --->UIViewController-2(ViewController associated with ContainerView) .

5
It seems that you got an answer here: stackoverflow.com/a/18673394/1187415. I just wonder if that is the same what @Mundi said below.Martin R
Don't post the same question twice. I'm tempted to close this thread as a duplicate.Duncan C
@DuncanC I'd be inclined to close the other one, as it's the duplicate of this one. This one was first (and is more explicit).Rob

5 Answers

28
votes

Ok, let's imagine this scenario:

storyboard

And let's assume you want to update the label on that "child of second view controller" with the model data backing the cell you tapped on the table view.

What you can do is:

  1. Give the segue from the first scene to the second one a unique identifier (e.g. Detail), define a property in that second view controller to receive the value being passed to it (e.g. someStringValue), and write a prepareForSegue that passes the value, e.g.:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"Detail"])
        {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            [segue.destinationViewController setSomeStringValue:self.objects[indexPath.row]];
        }
    }
    
  2. Repeat this process for your embed segue, namely, give your embed segue its own unique identifier (e.g. Embed) and create a property in that "child of second view controller" view controller to receive the value passed to it (e.g. someStringValue), and have a prepareForSegue in the second view controller that will pass the value along to its child view controller, e.g.:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"Embed"])
        {
            [segue.destinationViewController setSomeStringValue:self.someStringValue];
        }
    }
    
4
votes

As other's have said one can override prepareForSegue to locate the child view controller - personally I prefer using UIViewController.childControllers as you can access that at a time other than when the embedded segue occurs i.e.

-(void)viewDidLoad {
    for (UIViewController* vc in self.childViewControllers) {
            if ([vc isKindOfClass:MyChildController.class]) {
               // do something here
            }
    }
}

Not suggesting you do it but if you are working with storyboards the order of the childControllers array is exactly as the order in interface builder so you could directly refer to childViewControllers[0], [1]

1
votes

You are right about prepareForSegue. The destinationViewController will give you the destination view controller.

I do not see why you would need anything else. If you want the destination controller to contain another controller (why?), you can give the destination controller a @property that points to that controller, and you can read and set this property.

But the question remains -- why would you want to do that?

0
votes

If all you are doing is creating a simulated navigation bar then using a container view and a child view controller is needlessly complex and memory-intensive. Just create your simulated nav bar as a view in view controller 1, give it a label that contains your title, hook up the label as an outlet, and set the label as desired. Much, much cleaner and simpler.

-1
votes

Declaring the Cell Reuse Identifier and use This method.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

   if([segue.identifier isEqualToString:@"nameOfSegue"])
         {

          }


  }

visit the below link for more reference

Here