Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should or should not use nib files ?

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?

like image 483
Luong Huy Duc Avatar asked Jan 14 '12 17:01

Luong Huy Duc


People also ask

Should I use XIB or storyboard?

Storyboards - this is a visual tool for laying out multiple application views and the transitions between them (segues). XIBs (or NIBs) - each XIB file corresponds to a single view element and can be laid out in the Interface Builder, making it a visual tool as well.

What are nib files?

A nib file is a special type of resource file that you use to store the user interfaces of iOS and Mac apps. A nib file is an Interface Builder document.

What is difference between XIB and NIB?

NIBs and XIBs are effectively the same thing: XIBs are newer and are used while you're developing, whereas NIBs are what get produced when you create a build. In modern versions of iOS and macOS, NIBs and XIBs have effectively been replaced by storyboards, although you may still meet them if you work on older projects.


3 Answers

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?

like image 149
bryanmac Avatar answered Oct 20 '22 13:10

bryanmac


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.

like image 36
csotiriou Avatar answered Oct 20 '22 13:10

csotiriou


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.

like image 23
thomas Avatar answered Oct 20 '22 14:10

thomas