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];
}
}
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With