問題描述
在 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]
?Session 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?
是否需要在調用removeFromParentViewController
之后再調用didMoveToParentViewController:nil
?
Is it necessary to call didMoveToParentViewController:nil
after calling removeFromParentViewController
?
推薦答案
UIViewController
文檔非常清楚何時以及何時不調用 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:
方法.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.
祝你好運
這篇關于視圖控制器包含在 iOS 5 中是如何工作的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!