addChildViewController实际上做了什么? [英] What does addChildViewController actually do?

查看:118
本文介绍了addChildViewController实际上做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚第一次踏入iOS开发阶段,而我必须要做的第一件事就是实现一个自定义容器视图控制器 - 让我们称之为 SideBarViewController - 交换掉它显示的几个可能的子视图控制器中的哪一个,几乎完全像标准的 Tab Bar Controller 。 (它几乎是一个标签栏控制器,但有一个可隐藏的侧面菜单而不是标签栏。)

I'm just dipping my feet for the first time into iOS development, and one of the first things I've had to do is implement a custom container view controller - lets call it SideBarViewController - that swaps out which of several possible child view controllers it shows, almost exactly like a standard Tab Bar Controller. (It's pretty much a Tab Bar Controller but with a hideable side menu instead of a tab bar.)

根据Apple中的说明文档,每当我将一个子ViewController添加到我的容器时,我都会调用 addChildViewController 。用于交换当前子视图控制器的代码由 SideBarViewController 显示如下:

As per the instructions in the Apple documentation, I call addChildViewController whenever I add a child ViewController to my container. My code for swapping out the current child view controller being shown by the SideBarViewController looks like this:

- (void)showViewController:(UIViewController *)newViewController {
    UIViewController* oldViewController = [self.childViewControllers 
                                           objectAtIndex:0];

    [oldViewController removeFromParentViewController];
    [oldViewController.view removeFromSuperview];

    newViewController.view.frame = CGRectMake(
        0, 0, self.view.frame.size.width, self.view.frame.size.height
    );
    [self addChildViewController: newViewController];
    [self.view addSubview: newViewController.view];
}

然后我开始试图找出 addChildViewController 在这里,我意识到我不知道。除了在 .childViewControllers 数组中粘贴新的 ViewController 之外,它似乎对任何东西都没有影响。从孩子控制器的视图到我在故事板上设置的子控制器的操作和出口仍然正常工作,即使我从来没有调用 addChildViewController ,我无法想象还有什么可能会影响。

Then I started trying to figure out just what addChildViewController does here, and I realised that I have no idea. Besides sticking the new ViewController in the .childViewControllers array, it seems to have no effect on anything. Actions and outlets from the child controller's view to the child controller that I've set on the storyboard still work just fine even if I never call addChildViewController, and I can't imagine what else it could affect.

的确,如果我重写我的代码而不是调用 addChildViewController ,而是看起来像这...

Indeed, if I rewrite my code to not call addChildViewController, and instead look like this...

- (void)showViewController:(UIViewController *)newViewController {

    // Get the current child from a member variable of `SideBarViewController`
    UIViewController* oldViewController = currentChildViewController;

    [oldViewController.view removeFromSuperview];

    newViewController.view.frame = CGRectMake(
        0, 0, self.view.frame.size.width, self.view.frame.size.height
    );
    [self.view addSubview: newViewController.view];

    currentChildViewController = newViewController;
}

...然后我的应用程序仍能完美运行,据我所知!

... then my app still works perfectly, so far as I can tell!

Apple文档没有详细说明 addChildViewController 的作用,或为什么我们应该这样做叫它。有关该方法的相关描述的全部范围或其原因应在 UIViewController 类参考目前是:

The Apple documentation doesn't shed much light on what addChildViewController does, or why we're supposed to call it. The entire extent of the relevant description of what the method does or why it should be used in its section in the UIViewController Class Reference is, at present:


将给定的视图控制器添加为子视图。
...
此方法仅用于由自定义容器视图控制器的实现调用。如果你覆盖这个方法,你必须在你的实现中调用super。

Adds the given view controller as a child. ... This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must call super in your implementation.

在同一页面上还有这个段落:

There's also this paragraph earlier on the same page:


在将子视图的根视图添加到视图层次结构之前,容器视图控制器必须将子视图控制器与其自身关联。这允许iOS正确地将事件路由到子视图控制器以及这些控制器管理的视图。同样,在从视图层次结构中删除子视图的根视图后,它应该断开该子视图控制器与其自身的连接。要创建或中断这些关联,容器将调用基类定义的特定方法。这些方法不是由容器类的客户端调用的;它们只能由容器的实现来使用,以提供预期的包含行为。

Your container view controller must associate a child view controller with itself before adding the child’s root view to the view hierarchy. This allows iOS to properly route events to child view controllers and the views those controllers manage. Likewise, after it removes a child’s root view from its view hierarchy, it should disconnect that child view controller from itself. To make or break these associations, your container calls specific methods defined by the base class. These methods are not intended to be called by clients of your container class; they are to be used only by your container’s implementation to provide the expected containment behavior.

以下是您可能需要调用的基本方法:

Here are the essential methods you might need to call:

addChildViewController:

removeFromParentViewController

willMoveToParentViewController:

didMoveToParentViewController:

addChildViewController:
removeFromParentViewController
willMoveToParentViewController:
didMoveToParentViewController:

但是它没有提供关于它所谈论的'事件'或'预期遏制行为'是什么,或者为什么(甚至何时)调用这些方法是必要的的任何线索。

but it doesn't offer any clue as to what the 'events' or 'expected containment behavior' that it's talking about are, or why (or even when) calling these methods is 'essential'.

Apple文档的自定义容器视图控制器部分中的自定义容器视图控制器的示例都调用此方法,因此我认为它有一些重要的用途除了将子ViewController弹出到一个数组之外,我无法弄清楚这个目的是什么。这个方法做了什么,为什么要调用它?

The examples of custom container view controllers in the "Custom Container View Controllers" section of the Apple documentation all call this method, so I assume that it serves some important purpose beyond just popping the child ViewController onto an array, but I can't figure out what that purpose is. What does this method do, and why should I call it?

推荐答案

我也想知道这个问题。我观看了 WWDC 2011的会议102 视频和View Controller先生,< a href =https://twitter.com/twotothen =noreferrer> Bruce D. Nilo ,说:

I was wondering about this question too. I watched Session 102 of the WWDC 2011 videos and Mr. View Controller, Bruce D. Nilo, said this:


viewWillAppear: viewDidAppear:等与 addChildViewController无关: 。所有 addChildViewController:的确是说这个视图控制器是那个视图控制器的孩子,它与视图外观无关。当它们被调用时,它与视图进出窗口层次结构相关联。

viewWillAppear:, viewDidAppear:, etc have nothing to do with addChildViewController:. All that addChildViewController: does is to say "This view controller is a child of that one" and it has nothing to do with view appearance. When they get called is associated with when views move in and out of the window hierarchy.

所以似乎调用 addChildViewController:做得很少。呼叫的副作用是重要的部分。它们来自 parentViewController childViewControllers 关系。以下是我所知道的一些副作用:

So it seems that the call to addChildViewController: does very little. The side effects of the call are the important part. They come from the parentViewController and childViewControllers relationships. Here are some of the side effects that I know:


  • 将外观方法转发到子视图控制器

  • 转发轮换方法

  • (可能)转发内存警告

  • 避免不一致的VC层次结构,特别是在 transitionFromViewController:toViewController:... 其中两个VC需要拥有相同的父级

  • 允许自定义容器视图控制器参与状态保存和恢复

  • 参与响应者链

  • 连接 navigationController tabBarController 等属性

  • Forwarding appearance methods to child view controllers
  • Forwarding rotation methods
  • (Possibly) forwarding memory warnings
  • Avoiding inconsistent VC hierarchies, especially in transitionFromViewController:toViewController:… where both VCs need to have the same parent
  • Allowing custom container view controllers to take part in State Preservation and Restoration
  • Taking part in the responder chain
  • Hooking up the navigationController, tabBarController, etc properties

这篇关于addChildViewController实际上做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆