7

I'm new to iOS development. I've read through some of the source code I've found online and lots of them does not include a single nib file. All the view seems to be drawn manually from code.

What is the benefits of having/not having a nib file? Why did they choose to create everything from code instead of something you could visualize such as storyboard or *.xib files?

1
  • Don't forget that: "Storyboards are supported in iOS 5 and later and are enabled by default for new projects. If your app must run on earlier versions of iOS, though, you cannot use storyboards and should continue to use nib files." (iOs App Programming Guide ©Apple Inc).
    – Kenneth
    Feb 22, 2013 at 10:52

6 Answers 6

11

The biggest reason I like code is it diffs well in source control. XIBs are very hard to diff, merge, read.

Code is also easy that copy/paste saved snippets. With IB, I always forget some check box which leaves me wondering why the magic isn't working. My notes have code snippets.

What IB really excels at is layout and helping you with human interface guidelines (number of pixels between controls etc... with guiedlines).

So, my personal preference is layout in IB and everything else in code (including target, action, adding columns etc... etc...). Under dynamic scenarios though, IB falls apart and you end up with custom views.

Here's a related post:

Interface Builder (XIB) or Code when merging in a team environment?

9

There are many reasons on why you should use nibs and why you shouldn't. There is no definitive answer, and each answer depends on what you need to do.

Apart from the obvious advantages Nibs offer (speedy UI creation process, minimize view construction code in .m files) they offer something you can't find any other way: Localization problems solving. While localizing your application to other languages, you will stumble across phrases and things that take 2-3 words to explain while in another language they take just one. That seriously leads to errors with misplaced views inside a view controller when using different localizations. So, you can have 2-3 sub-nibs for each nib in Xcode 4, and localize every one the way you like it, and place the buttons and views in the correct spots, without worrying about relocating your views depending on the language the user has. If you were to do all this using code, you should have placed 'if's everywhere, and that is certainly a bad programming practice, and error prone.

I have created some view controllers that would require hundreds of lines just to set up the views if I didn't use interface builder.

However, NIBs will never achieve the performance of creating the views programmatically since every NIB is a view descriptor written in HTML/XML and before any view is created, a file must be read from the disk and analyzed. Nibs also lack the customization options that the plain code has (drop shadows, round corners, and other Quartz magic). These customization options are not available because there are many ways to achieve the same result using code, either by talking to the higher level Core Animation layer, either by addressing QuartCore and CGGraphics directly and doing heavy stuff there which is certainly faster and recommended in most cases (shadows using layers can be extremely slow). So Apple does not want to constraint development into a specific way of drawing things.

NIBs exist for a reason. You must make sure that in your application you understand the reasons you create a NIB. Nibs exist to connect code to outlets, facilitate localization, speed up development and clean up your code. Inside a project you must certainly use Nibs, but you must also avoid using them where plain code would also give you the same results with minimum or similar effort.

Last but not least, take memory management into consideration. Using Nibs will affect the deallocation of allocated objects like IBOutlets. If you arena's sure that an IBOutlet you create will be deallocated when you want to, do not use a NIB. use a plain code instead.

7

there are many reasons to create views in code.

  • nib-files are lazy-loaded and lead sometime to a noticeable lack of reaction which users dont like
  • you cannot configure everything in IB and a lot Views need some fancy extras
  • sometimes it is easier to just write your view down than clicking and dragging all needed stuff together
  • ...

I think the most important reason is the lack of performance and features.

I am using nib-files when I just want to show easy information with just simple buttons and labels and for prototyping.

5

There are very simple nibs like the typical MainWindow.nib, which don't have localized content and can be reproduced with a single line of code. In such cases code is much faster than unarchiving the nib.

There are also highly dynamic layouts which are impossible to describe as a nib.

You should choose the most convenient and efficient way in each particular case. There will always be tradeoffs, just choose wisely.

1

A nib file is a special type of resource file that you use to the user interfaces of iOS and Mac apps. A nib file is an Interface Builder document. You use Interface Builder to design the visual parts of your app—such as windows and views—and sometimes to configure non-visual objects, such as the controller objects that your app uses to manage its windows and views. In effect, as you edit an Interface Builder document, you create an object graph that is then archived when you save the file. When you load the file, the object graph is unarchived.

The nib file—and hence the object graph—may contain placeholder objects that are used to refer to objects that live outside of the document but that may have references to objects in the document, or to which objects in the document may have references. A special placeholder is the File’s Owner.

At runtime, you load a nib file using the method loadNibNamed:owner: or a variant thereof. The File’s Owner is a placeholder in the nib file for the object that you pass as the owner parameter of that method. Whatever connections you establish to and from the File’s Owner in the nib file in Interface Builder are reestablished when you load the file at runtime.

iOS uses nibs as an implementation detail that supports storyboards, the iOS user interface design layout format. Storyboards allow you to design and visualize the entire user interface of your app on one canvas. For iOS developers, using storyboards is the recommended way to design user interfaces.

0

I would start with xibs. Once you have finalized the UI, then migrate your xibs to code. This way you get the best of both worlds. XIBs can be slow, and I've seen 400KB XIBS (kinda rare though). XIBs are definitely crufty though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.