简体   繁体   中英

Getting reference to the view of a container view

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) .

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:

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

     - (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 (eg Embed ) and create a property in that "child of second view controller" view controller to receive the value passed to it (eg someStringValue ), and have a prepareForSegue in the second view controller that will pass the value along to its child view controller, eg:

     - (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 ie

-(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]

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM