问题描述
我有一个相当复杂的视图,对我来说,它有一些带有自定义界面项目的托盘".它们滑入和滑出我的根视图.我想在视图中嵌套(addSubview)项目.每个都需要在显示之前进行一些设置……并且不能在 IB 中进行配置(它们是 UIView 的子类).
I've got a fairly complex view, for me anyway, that has a few "trays" with custom interface items on them. They slide in and out of my root view. I'd like to nest (addSubview) the items inside the view. Each needs some setup before being displayed...and none can be configured in IB (they're subclasses of UIView).
我想知道为每个托盘"子类化 UIViewController 是否有意义,然后让 VC 的视图属性指向托盘"视图,我可以用我的自定义 UIView 对象填充它.这样我就可以利用 UIViewController 中的 viewDidLoad 等方法.
I'm wondering if it makes sense to subclass UIViewController for each "tray" and then have the VC's view property point to the "tray" view which I can populate with my custom UIView objects. This way I can leverage the viewDidLoad, etc... methods in UIViewController.
我不知道其他人这样做 - 至少在我看过的几个样本中.这会造成一种情况,即屏幕上同时显示多个视图控制器.从导航控制器本身一直到 rootViewController 及其视图,然后是任意数量(好吧,屏幕大小允许)的这些小托盘视图控制器.如果是这样,响应者链是如何工作的?我假设它会从最低的 UIView 到它的封闭 VC,然后到那个 VC 的父视图,然后是那个视图的 VC 等等.重复,重复..直到 UIApplication ......我是在自找麻烦吗?
I'm not aware of others doing this - at least in the few samples I've looked at. It would create a situation where there would be multiple view controllers being displayed on the screen at once. from the Navigation controller itself on down to the rootViewController and its view and then any number (well, screen size permitting) of these small trayViewControllers. If so, how's the responder chain work? i assume it'd go from lowest UIView to its enclosing VC, then to that VC's parent view, then that view's VC, etc. etc. repeat, repeat.. up to UIApplication... am I asking for trouble?
或者,我是否只是坚持使用 UIViews 并将子视图添加到子视图中等等.
OR, do I just stick with UIViews and adding subviews into subviews, etc. etc..
推荐答案
在 iOS 5.0 之前,特别不推荐这样做,因为嵌套视图控制器的生命周期事件(viewWillAppear 等)不会被调用.请参阅滥用 UIViewControllers.
Prior to iOS 5.0 this will specifically not recommended because the nested view controllers' lifecycle events – viewWillAppear, etc. – won't be called. See Abusing UIViewControllers.
当多个 UIViewController 的视图同时可见时,其中一些控制器可能不会收到像 -viewWillAppear: 或 -didReceiveMemoryWarning 这样的重要消息.此外,它们的某些属性(如 parentViewController 和 interfaceOrientation)可能无法按预期设置或更新.
iOS 5.0 添加了 包含 UIViewControllers,通过添加子视图控制器正确处理这些生命周期事件.
iOS 5.0 added containment UIViewControllers that correctly handles those lifecycle events by adding child view controllers.
- (void)addChildViewController:(UIViewController *)childController
我花了无数个小时试图让嵌套的视图控制器在 iOS 4 中工作.我最终做到了,但它需要大量容易出错的胶水代码.然后我在文档中看到了警告.
I spent countless hours trying to get nested view controllers to work in iOS 4. I eventually did, but it required a lot of glue code that was easy to get wrong. Then I saw the warning in the docs.
这篇关于“筑巢"是否明智?UIViewControllers 在其他 UIViewControllers 中,就像你在 UIViews 中一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!