理解 addChildViewController 的使用 [英] Understanding the use of addChildViewController

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

问题描述

我正在处理一些需要重构的代码.一个视图控制器充当其他两个视图控制器的容器,并将在它们之间进行交换,如下面的代码所示.

I'm working with some code that I need to refactor. A view controller is acting as a container for two other view controllers, and will swap between them, as shown in the code below.

这可能不是最好的设计.可能不需要以这种方式交换视图控制器.我明白那个.但是,当我使用此代码时,我想进一步了解 addChildViewController 调用会发生什么.我无法在 Apple 的文档或相关问题中找到答案,在这里(可能表明设计需要更改).

This may not be the best design. Swapping the view controllers in this way might not be required. I understand that. However, as I work with this code I want to further understand what happens with the addChildViewController call. I haven't been able to find the answer in Apple's docs or in related questions, here (probably an indication that the design needs to change).

具体来说 - 容器视图控制器如何处理被要求添加一个它已经添加的子视图控制器的情况?它是否认识到它已经添加了该视图控制器对象?

Specifically - how does the container view controller handle a situation where it is asked to add a child view controller, which it has already added? Does it recognise that it has already added that view controller object?

例如如果下面的代码在一个方法中 - 并且该方法被调用两次...

E.g. if the code below is inside a method - and that method is called twice...

[self addChildViewController:viewControllerB];
[self.view addSubview:viewControllerB.view];
[viewControllerB didMoveToParentViewController:self];

[viewControllerA willMoveToParentViewController:nil];
[viewControllerA.view removeFromSuperview];
[viewControllerA removeFromParentViewController];

谢谢,盖文

推荐答案

一般来说,他们的视图控制器包含"指南,当一个包含另一个时,应遵循以确定您是否需要实施包含.

In general, their guidelines for view controller "containment", when one contains another, should be followed to determine whether you will need to implement containment.

特别是,担心两次添加相同的子视图控制器就像担心两次呈现相同的视图控制器一样.如果你真的考虑清楚了,你就不需要面对这个问题.你的预感是正确的.

In particular, worrying about adding the same child view controller twice is like worrying about presenting the same view controller twice. If you've really thought things through, you shouldn't need to face that problem. Your hunch is correct.

我同意 Apple 的文档应该更预先说明奇怪的参数会发生什么或不按顺序调用时会发生什么,但也可能是不想将自己绑定到会导致麻烦的纠错设计的情况在路上.当您设计出的设计永远不会以错误的方式调用这些方法时,您就可以正确地解决问题并使自己独立于他们可能有或没有的任何纠错 - 如果您考虑到这一点,则更重要,因为它不是已记录在案,该错误纠正在未来可能会以不同的方式运行,从而破坏您的应用.

I agree that Apple's docs should be more up-front about what happens with weird parameters or when called out of sequence, but it may also be a case of not wanting to tie themselves to an error-correcting design that will cause trouble down the road. When you work out a design that doesn't ever call these methods in the wrong way, you solve the problem correctly and make yourself independent of whatever error correction they may or may not have - even more important if you consider that, since it's not documented, that error correction may work differently in the future, breaking your app.

再深入一点,您会注意到 Apple 的容器视图控制器无法进入无效状态(至少使用公共 API 不容易).使用 UITabViewController,从一个视图控制器切换到另一个视图控制器是一个原子操作,选项卡视图控制器在任何时间点都知道发生了什么.它要做的最多的事情就是删除活动的并显示新的.唯一一次它把所有东西都吹出水面的时候是当你告诉它你应该把所有东西都吹出水面并开始使用这些视图控制器"时.

Going even a bit further, you'll notice that Apple's container view controllers can't get in an invalid state (at least not easily with public API). With a UITabViewController, switching from one view controller to another is an atomic operation and the tab view controller at any point in time knows exactly what's going on. The most it ever has to do is remove the active one and show the new one. The only time where it blows everything out of the water is when you tell it "you should blow everything out of the water and start using these view controllers instead".

为其他任何事情编码,例如删除所有视图或所有视图控制器,无论在某些情况下看起来方便还是健壮,但实际上恰恰相反你的代码的一端不信任你的代码的另一端来保留它的部分交易.在任何实际对您有帮助的情况下,这意味着您让人们随意添加视图控制器而没有您应该想要的控制,在这种情况下,这就是您应该解决的问题.

Coding for anything else, like removing all views or all view controllers no matter what may in some cases seem expedient or robust, but it's quite the opposite since in effect one end of your code doesn't trust the other end of your code to keep its part of the deal. In any situation where that actually helps you, it means that you've let people add view controllers willy-nilly without the control that you should desire, and in that case, that's the problem you should fix.

这篇关于理解 addChildViewController 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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