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
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) .
Ok, let's imagine this scenario:
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:
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]];
}
}
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];
}
}