View Controllers - XIB or programmatically


I just wanted to confirm something after starting this section.

page 135 at the bottom.

“…if the view has no subviews, create it programmatically; if it has subviews, create a XIB file”

Ok so in my situation i fall in neither category. I am going to have a tab controller but how many tabs within that one tab controller I won’t know until runtime. For instance I have, somewhere on disk some directories that are labeled according to what type of images are within each directory. You might have “grass”, “buildings”, “ground”, “rocks” etc. and within each of those directories you are going to have 1…x number of images which will also be unknown until runtime. So you have an unknown number of image directories and and unknown number of images within each directory…but you want to build a Tab Controller -> Tabs ->Images for each Tab("directory) structure in your app and some buttons to manipulate other aspects of the app somewhere in there.

I am assuming that all this would have to be done programmatically but on 135 it didn’t really talk about why you wouldn’t want to create it programmatically if it had subviews. Will i get to why or why not later or can someone elaborate on this ? thanks!


Do not take it literally.
You can always create any view programmatically if you know where to put things and what their superviews are.
The key to this lies in setting the frame property of a view and then adding it to its superview.


It’s important to remember that just about anything that can be done in IB can be done programmatically.

The question is about tradeoffs. The things that IB can do for you, it can often do faster than if you’d done the same task programmatically. As you develop your skills, you will find a balance between tasks that you are able to accomplish more quickly and easily in code vs. those you prefer to accomplish using interface builder.

In the meantime, the quote you mention is a handy (if simplistic) rule of thumb that will help you get experience in both realms without wasting too much time.