What are the benefits of using Storyboards instead of xib files in iOS programming? What are the benefits of using Storyboards instead of xib files in iOS programming? ios ios

What are the benefits of using Storyboards instead of xib files in iOS programming?


A Storyboard is:

  • A container for all your Scenes (View Controllers, Nav Controllers, TabBar Controllers, etc)
  • A manager of connections and transitions between these scenes (these are called Segues)
  • A nice way to manage how different controllers talk to each other
  • Storyboards give you a complete look at the flow of your application that you can never get from individual nib files floating around.
  • A reducer of all the "clutter" that happens when you have several controllers each with it's own nib file.

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.


There are not only pro sides of Storyboarding, also cons - just because you asked for input:

  • it's not easy to work with SBs in a team, since only one participant can work on the SB at once (because it's one file).

-The following is not true: - if you need to do things SB doesn't offer, it's not quite easy to get SB mixed with programatical created views (well, it is possible though)

The rule of thumb seems to be: the more complex you expect your project to get, the more you'll better not go for SB.

EDIT: - another disadvantage of SB: working around all the annoying bugs of XCode regarding SB. E.g. having to frequently flush the DerivedData folder because of several inconsistencies. Sometimes storyboard files or the link to them get corrupted. Then you might have the joy to search for the problem. Take a look at this thread to get the idea

EDIT 2 (March 2013): meanwhile Storyboards and Xcode are working much better, and documentation and best practices are wide spread. I think working with storyboard can be recommended for a majority of projects, even if there are still some glitches.

EDIT 3 (Sept 2013): now with the new Xcode 5 format working in teams with SB might get even better, as it seems to become possible to merge SB-code much easier now.

Another EDIT: well, if you have an hour of time, sit back, relax and listen to these guys discussing this topic (Ray Wenderlich & Co)

Edit 2016.1: after a long time being a Storyboard advocate, I had so much hassle with it the last months, that I decided to abandon Storyboards as far as possible. The reason for that is that Apple adds feature like stupid, but doesn't care about the bugs and the flaws. The performance having a lots of auto layout constraints is really bad (while design time), and the error-prone-ness has become huge.Example: even less complex Storyboards tend to get into a 'dirty mode' right after opening a project in Xcode (see git state).Tip: as a beginner you will love Storyboards since you can prototype quickly and get things running without lots of code. As you enter an intermediate state, you'll add more GUI code to your project. Now you start going back and forth between code and SB - and things start working out worse. Sooner or later you'll tend to do most of the GUI stuff in code, because the result is more predictable than having several sources.


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 a UIViewController subclass by settings the class of File's Owner to any subclass of UIViewController 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.

  1. Storyboards have zooming capability which Nibs lack. Seriously, you can't zoom at all in Nibs which sucks when designing for bigger screens on a small laptop.
  2. Nibs are missing key functionality like:
    • Prototype and dynamic cells for UITableView (more info)
    • The top layout guide property (see comment)
    • There are probably more, please edit or comment if you have something to add to this list
  3. You don't need to mess with setting the class of Files Owner.

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

+ (ViewController *)create{    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ViewController" bundle:nil];    return [storyboard instantiateInitialViewController];}

Nib

+ (ViewController *)create{    return [super initWithNibName:@"ViewController" bundle:nil];}

Usage

- (void)showMyViewController{    ViewController *vc = [ViewController create];    [self presentViewController:vc animated:YES completion:nil];}

Swift

Storyboard

static func create() -> ViewController {    let storyboard = UIStoryboard(name: "ViewController", bundle: NSBundle.mainBundle())    return storyboard.instantiateInitialViewController() as! ViewController}

Nib

static func create() -> ViewController {    return ViewController(nibName: "ViewController", bundle: nil)}

Usage

func showMyViewController() {    let vc = ViewController.create()    self.presentViewController(vc, animated: true, completion: nil)}

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

  1. Teams and merging

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

  1. Complexity

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.

  1. Reuseability

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.