问题
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) .
回答1:
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 aprepareForSegue
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 aprepareForSegue
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]; } }
回答2:
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]
回答3:
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?
回答4:
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.
回答5:
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
来源:https://stackoverflow.com/questions/18672590/getting-reference-to-the-view-of-a-container-view