视图控制器包含如何在 iOS 5 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8379759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does View Controller Containment work in iOS 5?
提问by Gregory Higley
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.
在 WWDC 2011 Session 102 中,Apple 引入了 View Controller Containment,即创建自定义视图控制器容器的能力,类似于UITabBarController
、UINavigationController
等。
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.
我看了好几遍这些例子。有一系列与这种模式相关的方法,但要准确地弄清楚它们有点困难。我将在这里发布我认为正在发生的事情,看看社区是否会确认或否定我的怀疑。
Scenario 1: Moving from no parent to a new parent view controller
场景 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?
前两行必须按照给定的顺序出现,还是可以颠倒?
Scenario 2: Moving from a parent view controller to no parent view controller
场景 2:从父视图控制器移动到无父视图控制器
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
Is it also necessary to call [vc didMoveToParentViewController:nil]
? The examples in Session 102 did not do thisin this scenario, but I don't know whether that was an omission or not.
还需要打电话[vc didMoveToParentViewController:nil]
吗?Session 102 中的示例在这种情况下没有这样做,但我不知道这是否是遗漏。
Scenario 3: Moving from one parent view controller to another
场景 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];
Questions
问题
My main question is this: Is this how view controller containment should work, in general? Are the mechanics given above correct?
我的主要问题是:一般来说,视图控制器包含应该如何工作?上面给出的机制是否正确?
Is it necessary to call willMoveToParentViewController
before calling addChildViewController
? This seems like the logical order to me, but is it strictly necessary?
打电话willMoveToParentViewController
之前有必要打电话addChildViewController
吗?这对我来说似乎是逻辑顺序,但这是绝对必要的吗?
Is it necessary to call didMoveToParentViewController:nil
after calling removeFromParentViewController
?
打电话didMoveToParentViewController:nil
后一定要打电话removeFromParentViewController
吗?
采纳答案by timthetoolman
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.
该UIViewController
文档是在何时以及何时不呼叫很清楚willMove
/didMove
方法。查看“实现容器视图控制器”文档。
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."
文档说,如果您不覆盖addChildViewController
,则不必调用 willMoveToParentViewController:
方法。但是,您确实需要didMoveToParentViewController:
在转换完成后调用该方法。“同样,willMoveToParentViewController:
在调用方法之前调用removeFromParentViewController
方法是容器视图控制器的责任。该removeFromParentViewController
方法调用didMoveToParentViewController:
子视图控制器的方法。”
Also, there is an example worked out hereand sample code here.
Good Luck
祝你好运
回答by nevan king
This part is not correct:
这部分不正确:
[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view]; // or something like this.
[vc didMoveToParentViewController:self];
When your custom container calls the addChildViewController: method, it automatically calls the willMoveToParentViewController: method of the view controller to be added as a child before adding it.
当您的自定义容器调用 addChildViewController: 方法时,它会在添加之前自动调用要添加为子项的视图控制器的 willMoveToParentViewController: 方法。
So you don't need the [vc willMoveToParentViewController:self]
call. It is done automatically when you call [self addChildViewController:vc]
. Here's the code sample again:
所以你不需要[vc willMoveToParentViewController:self]
打电话。当您调用 时,它会自动完成[self addChildViewController:vc]
。这是代码示例:
[self addChildViewController:vc];
// [vc willMoveToParentViewController:self] called automatically
[self.view addSubview:vc.view]; // or something like this.
[vc didMoveToParentViewController:self];
For removing view controllers:
The removeFromParentViewController method automatically calls the didMoveToParentViewController: method of the child view controller after it removes the child.
removeFromParentViewController 方法在移除子视图控制器后自动调用子视图控制器的 didMoveToParentViewController: 方法。
Presumably this call is [oldVC didMoveToParentViewController:nil]
.
想必这个电话是[oldVC didMoveToParentViewController:nil]
.
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
// [vc didMoveToParentViewController:nil] called automatically