In iPhone in Action we push heavily on the idea of doing as much of your setup work as possible in Interface Builder. I continue to think that's a notable advantage that you have when using Apple's IDE: it can simplify lots of work and kickstart your programming by saving you from all the boring drudge work at the start.
However, there are inevitably situations when Interface Builder isn't the right solution. I hit one of those recently when I was writing a program that popped up a UITextField in response to some user interaction. Sure, I could have created the UITextField in Interface Builder, hidden it, and showed it only when the UI demanded it, but that seemed like I'd be using Interface Builder just for the sake of using it, rather than it actually being (in this case at least) the best solution.
So instead I created the UITextField by hand.
Now in the book we don't worry too much about how to create the UI objects by hand, because we figure that it's usually just a combination of an alloc with an init, following a standard pattern that we do discuss. However, when I did that for the UITextField, I discovered that I didn't have anything actually showing (though the text field did exist). That's because UITextField is one of those objects that requires some extra setup to make it work. For whatever reason, Apple didn't initialize it with defaults that did the job.
Once you've done an alloc and an initWithFrame:, you need to do at least the following to have a good-looking UITextField:
yourTextField.backgroundColor = [UIColor clearColor];
yourTextField.borderStyle = UITextBorderStyleRoundedRect;
Of course, you'll usually want to set a delegate and a returnKeyType too, but that's the same whether you create a UITextField by hand or using Interface Builder. See pages 257-259 of iPhone in Action for those discussions.

Comments