When to use InterfaceBuilder to build views?

大城市里の小女人 提交于 2019-12-12 09:46:39

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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




回答8:


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.



来源:https://stackoverflow.com/questions/3110440/when-to-use-interfacebuilder-to-build-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!