当我将一个视图添加到另一个视图(并且这两个视图都由视图控制器控制)时,我真的应该使用 addChildViewController 吗? [英] Shall I really use addChildViewController when I add a view to another view (and both these views are controlled by view controllers)?
问题描述
我正在构建一个带有 XIB 的 UIViewController
.在这个 XIB 中,我有占位符(XIB 中的 UIView
对象),我可以在其中放置其他视图,来自其他视图控制器.(顺便说一句,我也可以直接添加这些视图,不用占位符)
I am building a UIViewController
with a XIB. In this XIB, I have placeholders (UIView
objects in the XIB) in which I place other views, from other view controllers. (By the way, I could also add these views directly, without placeholders)
我不是在构建容器视图控制器:我的意思是它不是 UINavigationController
.它只是一个普通"的视图控制器.例如,想象一下,我的视图中有一个小的子视图,用于显示Facebook"徽标和计数器.此facebook 视图"附加到另一个视图控制器,当触摸Facebook 视图"时调用该视图控制器.因此,Facebook 控制器"肯定需要是我的主"视图控制器的 @property
.
I am not buiding a container view controller: I mean it's not a UINavigationController
. It's a just a "usual" view controller. Imagine for example that I have a small subview in my view for a "Facebook" logo and a counter. This "facebook view" is attached to another view controller that is called when the "Facebook view" is touched. So, the "Facebook controller" definitely needs to be a @property
of my "main" view controller.
在这种情况下,我真的应该使用 addChildViewController:
和所有机制吗?还是没有?
In this situation, should I really use addChildViewController:
and all the mechanism? Or no?
谢谢!
推荐答案
你不应该仅仅使用 UIViewController 来钓鱼"到 .xib 文件中以获得视图.如果这就是你想要的,不要那样做.只需加载笔尖并拉出视图,直接:
You should not be using a UIViewController merely to "fish" into a .xib file for you to obtain the view. If that's all you want it for, don't do that. Just load the nib and pull out the view, directly:
NSArray* objects = [[UINib nibWithNibName: @"MyNib" bundle: nil]
instantiateWithOwner:nil options:nil];
UIView* v = (UIView*)[objects firstObject];
但是如果您出于某些其他原因将 UIViewController 与此 .xib 文件结合使用,例如为了让它保持活动状态,以便 .xib 中的按钮可以向此 UIViewController 发送消息,那么绝对必须建立适当的父子关系,正如我在书中所描述的:http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers
But if you are using a UIViewController in conjunction with this .xib file for some other reason, e.g. to keep it alive so that a button inside the .xib can send messages to this UIViewController, then absolutely you must make a proper parent-child relationship, as I describe in my book: http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers
这篇关于当我将一个视图添加到另一个视图(并且这两个视图都由视图控制器控制)时,我真的应该使用 addChildViewController 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!