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 v开发者_C百科iew 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 calledinitWithNibName:bundle:
. I can't callinitWithFrame:
right?
Answers would be much appreciated!
Thanks, PhpMyCoderEDIT
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 withinitWithFrame:
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 tosetFrame:
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.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:
[[NSBundle mainBundle] loadNibNamed:@"foo" owner:self options:nil];
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 take view
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 that view.frame
is in the superview's coordinate system.
精彩评论