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?
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.
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.
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.
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?
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.
there are many reasons to create views in code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With