Going thru both the extensive online documentation AND the various code samples in the dev center I am perplexed. Apple's recommends to ALWAYS use IB when creating your views, and yet, in many of the code samples, views are created entirely in code (initialized in the loadView method of the viewController). Is there a 'best practice' as to when you should use IB as opposed to code to create your views?


I think if you need to create view for the whole screen, IB is a good choice. It is easier to maintain, fix bug.

If you just want to do a simple, quick view and need to create as subview in some position, you should go to code.

I think that is good enough for most cases


You might be confused. IB designs the view's layout, whereas the code instantiates the view object.

If you're asking about creating layout and instantiation programmatically vs loading the view from a nib/bundle... that's a bit out of my depth. But I recommend loading bundles/nibs vs programmatic unless you're doing fancy OpenGL stuff and changing lots of modes between orientation and system (iPad/iPhone). IB might not be able to help you there...

Stephen Furlani

Personally, I would recommend creating views purely using code. It gives you an incredible amount of control over the appearance of all the objects in a view, and makes objects a lot easier to access when events are fired or responses are needed.

From personal experience, I always tend to forget to link something from the File Owner's in IB and end up spending hours trying to finding the right little circle to re-connect. Go the all code way.

+1  A: 

From the book iPhone Programming: The Big Nerd Ranch Guide

If the view has no subviews, create it programmatically. If it has subviews, create a XIB file (ie use Interface Builder).

+1  A: 

Personal experience - IB all the way. I prefer to see what I am actually doing instead of guessing what ([myView size].height / 2 + 15) % 3 would look like.

Steam Trout
+1  A: 

I always start with Interface Builder. Dropping down into code is sometimes necessary, but in the early stages of view design the visual layout of IB makes prototyping orders of magnitude quicker and easier.

+2  A: 

There are no hard and fast rules, but in general, the more complex a view is, the more I'll tend to use IB. Very simple things like a TableView in a NavigationView will nearly always be in code. More complex layouts like a detail view with dozens of output labels will tend to be laid out in IB.

That said, if for some reason I don't want to include the XIB in the final product, such as if this is for a static library, I'll start with IB and then use a NIB to code converter.

John Franklin