开发者

When to use InterfaceBuilder to build views?

Going thru both the extensive online documentation AND the various code samples in the d开发者_开发技巧ev 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?


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.


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.


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).


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.


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.


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...


I'm working on a project right now and just found a specific case for using code instead of Interface Builder. I'm getting data from a web service, and the data determines how many UILabels I want on my view. Additionally, if I need two UILabels instead of one, everything below them needs to slide down on the screen. There's no way to tell Interface Builder to do this.

In summary, if you have a variable number of subviews or the positions of subviews may change based on your data, use Objective-C instead of Interface Builder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜