View Controller Containment如何在iOS 5中运行? [英] How does View Controller Containment work in iOS 5?
问题描述
在WWDC 2011 Session 102中,Apple推出了View Controller Containment,它能够创建自定义视图控制器容器,类似于 UITabBarController
, UINavigationController
等等。
In WWDC 2011 Session 102, Apple introduced View Controller Containment, which is the ability to create custom view controller containers, analogous to UITabBarController
, UINavigationController
, and the like.
我多次观看这些例子。有一系列与这种模式相关的方法,但要确切地弄清楚它们有点困难。我将在这里发布我认为正在发生的事情,看看社区是否会确认或否定我的怀疑。
I watched the examples several times. There are a flurry of methods associated with this pattern, but it was a little hard to figure them out exactly. I'm going to post here what I think is going on and see if the community will confirm or disconfirm my suspicions.
场景1:从没有父母移动到新的父视图控制器
[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view]; // or something like this.
[vc didMoveToParentViewController:self];
前两行必须按照给定的顺序发生,还是可以反转?
Do the first two lines have to occur in the order given, or can they be reversed?
场景2:从父视图控制器移动到无父视图控制器
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
是否还需要调用 [vc didMoveToParentViewController:nil]
?会话102 中的示例在这种情况下没有这样做,但我不知道这是否是遗漏。
Is it also necessary to call [vc didMoveToParentViewController:nil]
? The examples in Session 102 did not do this in this scenario, but I don't know whether that was an omission or not.
场景3:从一个父视图控制器移动到另一个父视图控制器
这可能会以下列方式发生,因为每个父视图控制器中的逻辑将封装。
This will likely occur in the following way, because the logic in each parent view controller will be encapsulated.
// In the old parent
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
// In the new parent
[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
问题
我的主要问题是:一般来说,视图控制器包含应该如何工作?上面给出的机制是否正确?
My main question is this: Is this how view controller containment should work, in general? Are the mechanics given above correct?
在调用 addChildViewController之前,是否需要调用
?这对我来说似乎是合乎逻辑的顺序,但这是非常必要的吗? willMoveToParentViewController
Is it necessary to call willMoveToParentViewController
before calling addChildViewController
? This seems like the logical order to me, but is it strictly necessary?
是否需要调用 didMoveToParentViewController:nil
调用 removeFromParentViewController
?
推荐答案
UIViewController
docs非常清楚何时何何不调用 willMove
/ didMove
方法。查看实现容器视图控制器文档。
The UIViewController
docs are pretty clear on when and when not to call willMove
/didMove
methods. Check out the "Implementing a Container View Controller" documentation.
文档说,如果你不覆盖 addChildViewController
,你不必调用 willMoveToParentViewController:
方法。但是,您需要在转换完成后调用 didMoveToParentViewController:
方法。 同样,在调用 removeFromParentViewController
方法之前,容器视图控制器负责调用 willMoveToParentViewController:
方法。 。code> removeFromParentViewController 方法调用子视图控制器的 didMoveToParentViewController:
方法。
The docs say, that if you do not override addChildViewController
, you do not have to call willMoveToParentViewController:
method. However you do need to call the didMoveToParentViewController:
method after the transition is complete. "Likewise, it is is the responsibility of the container view controller to call the willMoveToParentViewController:
method before calling the removeFromParentViewController
method. The removeFromParentViewController
method calls the didMoveToParentViewController:
method of the child view controller."
Also, there is an example worked out here and sample code here.
祝你好运
这篇关于View Controller Containment如何在iOS 5中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!