What are the main differences between using Storyboards and xib files.
Specifically, what are the advantages or disadvantages of using a Storyboard?
Unfortunately, despite doing quite a bit of research, all I've been able to find on Storyboards are simple tutorials that show you how to set up a Storyboard, instead of concrete information explaining what they are.
A Storyboard is:
I have been using Storyboards for awhile now and the ONLY downside is that you can't target iOS 4 or below. Storyboards only work on devices running iOS 5 or better. Other than that, the benefits are many and the downsides are non-existent IMO.
The best tutorial I have seen is Ray Wenderlich's
Also, if you are a member of the Apple Developer program, check out last years WWDC session on Storyboards (iTunesU), it is awesome.
Another great one (also on iTunesU) is the latest Stanford iOS Application Programming course.
A storyboard is basically a device to make your job as a developer easier. It is complied into a series of nib files, so the performance is pretty much equivalent, but it's great as a developer to be able to look at a quick overview of your entire application flow.
I'm starting to transition to using storyboards on new projects, providing I can convince the client to accept iOS 5 as a minimum version. This is purely because I prefer to do it this way, and it takes me less time to accomplish the same tasks.
Summary
Nibs/.xib files and Storyboards both are Interface Builder files which are used to visually create user interface for iOS and Mac applications in Xcode (i'll use iOS terminology for classes as this question is tagged iOS but it also applies to Mac programming).
Differences
Nibs are intended to be used with a single
UIView
. They can also be connected to aUIViewController
subclass by settings the class of File's Owner to any subclass ofUIViewController
and connection the view outlet (drag to connect using the Connections Inspector in the far right pane of Xcode).Storyboards are intended to contain the user interface for 1 or more
UIViewController
. You can build your entire user interface in a single storyboard or separate it into smaller parts.Advantages
Storyboards should always be used in favor of .xib files/Nibs (for view controllers). Storyboards have more features and are actively developed by Apple.
Every argument in favor of Nibs rely of the fact that they used individually while storyboards contain many scenes. You can use a single storyboard for each
UIViewController
just as easily as you can with Nibs (see code samples below). Keep reading for a detailed explanation and code examples.Detailed
Why are Storboards superior to Nibs?
The answer basically comes down to Apple encouraging the use of Storyboards and putting more development effort into them.
UITableView
(more info)The basic argument against storyboards is that having all your view controllers in one place leads to merge conflicts, a slow Xcode, slow build times and being a general pain in the butt to maintain. Hence, general advice is to use a Nib for each
UIViewController
.But... You can just create storyboard for each
UIViewController
. A common practice (for me at least) is to hide all the UIViewController initialization in a class method (as no other class needs to know the name of the file where the controller's Nib/Storyboard is located). Lets compare the related code snippets that one might use to create such a method. A single line of code is the entire difference between the two.Objective-C
Storyboard
Nib
Usage
Swift
Storyboard
Nib
Usage
Arguments
I'll address all the usual arguments for Nibs; as I mentioned earlier, there are mostly in favor of single files, not as an argument for Nibs over Storyboards
Argument: Having a storyboard with lots of view controllers will cause merge conflicts if you are working on a team with multiple people making changes
Response: A single storyboard causes no more merge conflicts than a single Nib
Argument: Very complex apps have a lot of scenes in the Storyboard which leads to a giant storyboard that takes forever to load and is barely comprehensible because of it's size.
Response: This is a great point, but you can easily break Storyboards into smaller parts. Storyboard References look like a great feature that can be used to link Storyboards together but they are only available in Xcode 7/iOS 9+. Also, still not a reason to choose individual Nibs over Storyboards.
Argument: Creating a Nib for each
UIViewController
subclass lets you reuse code so you don't have to setup all your constraints and outlets for each scene in your storyboard.Response: Again, not a reason to choose individual Nibs over individual Storyboards.
There was a nice presentation about Storyboard given at the LiDG meeting a couple of months ago.
Personally, I'd say it's the way to go with a new app. There are some gaps, especially for very complex apps, but the pro's mostly outweigh the cons.
Your attitude toward Auto Layout may also affect whether you want to use Storyboards. Using xibs you can enabled or disable Auto Layout is on a per .xib basis, allowing for a mix within your application, while Storyboards apply your choice to ALL views they contain.
You see the big picture in one second. Having many NIB files, well, you don't see the big picture. Easier to maintain your programs. Easier to understand others programs... among others.