Does storyboard eliminate the need for .nib

2019-01-11 10:26发布

问题:

With the introduction of storyboard, I wouldn't select to create a .xib/.nib when I create a subclass of UIViewController, because I can just drag out a viewcontroller in interface builder and assign it to the new class.

So, with storyboard, when would i need to use the .xib/.nib file?

Thank you.

回答1:

I guess, storyboard contains the .xib/.nib files for all your views. It present relationships between them and helps you to avoid confusion if you have a LOT of views. Also it saves your time and nerves when writing code.



回答2:

Storyboards don't completely remove the need for NIB files. One common example is when creating subviews to go inside a UIScrollView. You can't create the subviews separately in a storyboard; instead you have to create separate NIB(s) for the subviews and attach them to the scroll view programatically.

In fact, on almost any occasion where you have a need for subviews which change at runtime you'll want to use separate NIBs.

Storyboards are great for certain kinds of apps, but for anything even remotely complicated the old way works very well still. An additional benefit is that they co-exist nicely. You can put placeholder views in your storyboard and fill them programatically with NIB-defined views later on.



回答3:

I have tried out storyboarding lately and I do have mixed feelings about its use:

Pro's

  • Convenience: You might have to write much less code. So you can for example show a static view without having to write any code.
  • Overview: You can quickly see how the ViewControllers relate to each other
  • Efficiency: For the simple use cases I find the Storyboarding much more efficient than "the old way".

    Example 1: Editor > Embed In > Navigation Controller and voilà, no more instantiating and configuring is required.

    Example 2: You can make "Prototype Cells" for TableView which speeds up the creation of static table views dramatically. AFAIK this is not possible with nib files

  • It's cool :-)

Con's

  • I find that the re-usability somehow suffers with storyboards. Although I can segue to a given ViewController from multiple points, it is still limited since in one case I might use the VC as a Popover and in another as a Master-ViewController.
  • Assuming that you are working in a big team and different people are changing different parts of your application, having one monolithic storyboard which you then have to merge might pose a big problem team-work-wise.
  • I really would like to have some mechanism to include or reference existing nib files into the storyboard which is currently not possible.

So considering these points I still find storyboarding very appealing but it makes sense to combine both approaches which is not really a big deal. Additionally, this technology it is still quite new so it might improve in the (near) future.



回答4:

If you use storyboards, you generally won't need to create separate .xib files. You can still use a .xib file if you want to -- it might be useful e.g. if you have a particularly complex view that you'd like to configure by itself. Most of the time, though, there won't be a need to do that.