以编程方式容器视图控制器

Container View Controller Programmatically

我已经对此进行了一段时间的研究,但找不到我需要的东西。我想学习如何以编程方式创建带有子视图控制器的容器视图。我对此还很陌生并正在学习基础知识,但据我所知,这过去是使用可重用视图完成的,并将它们附加到子视图控制器,然后再将容器视图对象添加到库中(对吗?),我正在寻找一个教程或示例代码来说明如何使用 xib 从头开始​​执行此操作,但没有任何复杂性,例如添加 table 单元格等...只是以编程方式添加容器和子项。那有意义吗?我敢肯定 S.O 上一定有什么东西。如果你能帮忙,谢谢。

更新---------------------------------------- ---------------------------------------------- ---------------------- 我设法创建了一个带有 UIButton 操作的子视图控制器。相关代码:

- (IBAction)Pressed:(id)sender {
    ChildViewController *childViewController = [[ChildViewController alloc]init];
    [self displayContentController:childViewController];
}

- (void) displayContentController: (UIViewController*) content {
    [self addChildViewController:content];
    content.view.frame = CGRectMake(0, 115, 320, 240);
    content.view.backgroundColor = [UIColor redColor];

        CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[content.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}

所以一切正常。我单击该按钮,出现一个红色正方形子视图控制器,它占据了屏幕的一小部分。我想知道这是否是最佳做法。

基本上这涉及到有 1 个 Parent 视图控制器来协调他的子视图控制器的外观。

老实说,我觉得Apple Docs这部分很完整。

来自同一文档的引述:

Adding a Child View Controller to Your Content

To incorporate a child view controller into your content programmatically, create a parent-child relationship between the relevant view controllers by doing the following:

  1. Call the addChildViewController: method of your container view controller. This method tells UIKit that your container view controller is now managing the view of the child view controller.
  2. Add the child’s root view to your container’s view hierarchy. Always remember to set the size and position of the child’s frame as part of this process.
  3. Add any constraints for managing the size and position of the child’s root view.
  4. Call the didMoveToParentViewController: method of the child view controller.

代码示例:

- (void) displayContentController: (UIViewController*) content {
   [self addChildViewController:content];
   content.view.frame = [self frameForContentController];
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];
}

响应有关最佳实践的更新:

Objc.io 在这方面有一些非常简洁的文章。

比如文章说到View Controller Containment 注释:

View controllers should be reusable and self-contained entities.

Child view controllers are no exception to this rule of thumb. In order to achieve this, the parent view controller should only be concerned with two tasks: laying out the child view controller’s root view, and communicating with the child view controller through its exposed API. It should never modify the child’s view tree or other internal state directly.

Child view controllers should contain the necessary logic to manage their view trees themselves – don’t treat them as dumb views. This will result in a clearer separation of concerns and better reusability.

他们还讨论了使用此模式时视图控制器之间的 transitions

- (void)addViewControllerToContainer:(UIViewController *)viewController {

    [self addChildViewController:viewController];

    viewController.view.frame = self.container.bounds; //important!!
    [self.container addSubview:mapVC.view];

    [viewController didMoveToParentViewController:self];
}