Getting reference to the view of a container view

三世轮回 提交于 2019-11-30 05:16:16

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

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]

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!