I am reading an Xcode project that has a lot storyboards that contains only the view controller, just for creating VC instances. In the past, I would just use xib for the same purpose.
Either way works just fine for creating instance in the code. Just wondering if the single vc storyboard approach provide any advantage or disadvantage over xib. Love to hear any thoughts.
5
Answers
No there is no any advantage for single vc inside a storyboard over xib , as the storyboard will has a meaning when it contains many vcs to show the flow of app screens and partition the app to modules
Storyboards allow you to view multiple views together in a way not possible with a xib. For example, you can create a table view controller in a story board scene, then create and layout each cell inline to the table view. This way, you can see the layout of each cell relative to the other cells.
In addition, you can also create static table views if the number of rows and sections are not dynamic. A good example of this is a settings screen, in which the number of rows and sections do not change. You can actually create outlets for labels in each row and directly set their contents. This lets you avoid implementing table view delegate and datasource callbacks to populate the data.
However, those are fairly specific cases, so using a storyboard doesn’t always have an advantage. If you want to reuse a cell across different screens, you’ll want to use a xib, instead of copying and pasting that cell into each storyboard scene.
Also, I’d tend to use SwiftUI going forward as you tend to get the best of both worlds (programatic and visual layout.) For example, I’ve been able to recreate complex settings screens in a third of the code and time. And it’s significantly easier to maintain. YMMV.
Personally, I like using storyboards whether it contains
single ViewController
ormultiple ViewControllers
. Storyboard seems like a pictorial diagram to me. A storyboard with Single VC meant to me as a dead-end means you can’t go anywhere from the VC. If you work in a team, multiple Storyboards is a plus point, in case of merge conflict with others. So I don’t feel hesitant if I use multiple storyboards.But in the case of creating a view, I use
XIB
. It is easier to reuse them in different ViewControllers. Mind that, you won’t use the same viewController in multiple places, you use the views.Both .xib and Storyboard use decoder to initialize. As for performance gains, if there is any it’s completely negligible. You don’t actually need either to instantiate, although if you’re a visual person there can be some benefit to doing it that way. If you’re just making a simple controller, you can just keep your variable in whatever file and instantiate with code.
ie:
You can also present that controller from another or push to nav stack.
Disadvantages:
Advantage
its the only advantage I can think of