How does View Controller Containment work in iOS 5?(视图控制器包含在 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]
?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 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:视图控制器包含在 iOS 5 中是如何工作的?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01