最近,一位朋友向我介绍了 iOS 开发,并开始尝试界面生成器和视图控制器。我发现的一件事是,当将 nib 与视图控制器结合使用时,您的视图控制器 ivar 很快就会被您可能永远不会真正引用的视图污染。我想将主笔尖中的关键组件模块化为几个不同的视图。我对此有两个问题:
-
如何为自定义大小的视图创建一个笔尖文件(一个不会填满整个屏幕的视图)?
- 如何将新模块化的内容添加到我的主笔尖(我为组件创建的所有类都是视图控制器而不是视图)?
-
假设我要分配和初始化视图控制器,并以编程方式将它们添加到主视图中,那么我如何定位自定义大小的视图,因为我已经调用了 initWithNibName:bundle:
。我无法调用 initWithFrame:
对吗?
非常感谢您的回答!
谢谢,
PhpMyCoder
编辑
我已经找到了第一个问题的答案。似乎在 nib 文件的属性检查器中,您必须禁用状态栏(将其更改为未指定)才能在尺寸检查器中编辑高度和宽度参数。但是,我仍然不确定如何将这些自定义笔尖及其视图控制器添加到另一个笔尖,而不用 initWithFrame:
和 addSubview:
对其进行编码。关于将视图控制器的视图添加到 IB 中的笔尖有什么想法吗?
编辑2
添加了问题 3(或 2,具体取决于您的想法)。
编辑3
看来我今天问问题有点急了。对 setFrame:
的简单调用将处理大小调整和定位(您甚至可以将其附加到 init 函数 initWithNibName:bundle:frame:
上)。仍然不确定如何将视图(由笔尖创建)从视图控制器添加到 Interface Builder 中的另一个笔尖。如果可能的话,我很想听听如何实现。
I've recently been introduced to development for iOS by a friend and have begun to experiment with the interface builder and view controllers. One thing I'm finding is that when using a nib in conjunction with a view controller, your view controllers ivars are quickly polluted with views you may never actually reference. I would like to modularize the key components in my main nib into several different views. I have two questions regarding this:
How can I create a nib file for a custom sized view (one that doesn't fill the entire screen)?
- How can I add the newly modularized to my main nib (all of the classes I would create for the components would be view controllers not views)?
Assuming I were to alloc and init the view controllers and add them to the main view programmatically, how could I position the custom sized views since I have already called initWithNibName:bundle:
. I can't call initWithFrame:
right?
Answers would be much appreciated!
Thanks,
PhpMyCoder
EDIT
I've discovered the answer to my first question. It seems that in the attributes inspector of your nib file you must disable the status bar (change it to unspecified) to enable the editing of the height and width parameters in the size inspector. However, I still am unsure of how to add these custom nibs and their view controllers to another nib without coding them in with initWithFrame:
and addSubview:
. Any ideas on adding a view controller's view to a nib in IB?
EDIT 2
Added question 3 (or 2 depending on how you think about it).
EDIT 3
I seem to be hastily asking questions today. A simple call to setFrame:
will deal with sizing and positioning (and you can even append it on to your init function initWithNibName:bundle:frame:
). Still not sure how to add the view (created by a nib) from a view controller to another nib in Interface Builder. If this is possible, I'd love to hear how.
发布评论
评论(1)
记住不要混淆 ViewController 和 View。
它们都在层次结构中,每个 ViewController 都有/控制一个“主”视图(可能有一堆子视图)。
您不向笔尖添加视图。笔尖是一种帮助您组装视图的机制。 (如果我们深入研究命名法,则为 NeXT Interface Builder 文件。)
这就是加载笔尖的方式:
通常,您为控制器指定笔尖名称,它会为您执行此操作,但您可以这样做。访问内容的方法很复杂且棘手。以下是执行此操作的标准方法。
您传入的所有者必须是在该 nib 文件中声明为所有者的类型。它应该有一些出口连接到 nib 文件中的对象。加载 nib 文件后,它们就会“存在”。如果您调用此两次,它将覆盖第一个并用第二个替换它们。 (大多无害,绝对无用)
因此,通常,您将其连接到
view
。现在,您有一个在内存中浮动的视图,并且未连接到应用程序的视图层次结构。是时候这样做了。 您必须获取view
并找出它在预先存在的层次结构中的位置,然后调用[someOtherView addSubview:self.view]
不,它就会出现。是的,如果你想明确地放置/调整它的大小,你将需要这样做。请注意,view.frame
位于超级视图的坐标系中。Remember to not get ViewControllers and Views confused.
They are both in a hierarchy, and each ViewController has/controls a "main"
view
(which likely has bunches of subviews).You don't add a view to a nib. A nib is a mechanism to help you assemble views. (A NeXT Interface Builder file, if we delve into nomenclature.)
This is how you load a nib:
Normally, you give a nib name to a controller and it does it for you, but you can do this to. There are complex and tricky ways to access the content. The following is the standard way to do it.
The owner you pass in must be the type declared as the owner in that nib file. It should have some of its outlets connected to objects in the nib file. After you load the nib file, they'll just "be there". If you called this twice, it would overwrite the first ones and replace them with the second ones. (mostly harmless, definitely useless)
So, typically, you wired it up to
view
. Now you have a view that's floating around in memory and not connected to the view hierarchy of the application. Time to do that. You must takeview
and figure out where it belongs in the pre-existing hierarchy and call[someOtherView addSubview:self.view]
no it, and it will appear. Yes, if you want to explicitly place/size it, you will need to do that. Note thatview.frame
is in the superview's coordinate system.