skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. Personally, I like using storyboards whether it contains single ViewController or multiple 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.

    Login or Signup to reply.
  4. 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:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       
            let vc = UIViewController()
            vc.view.backgroundColor = .blue
    
            self.window?.rootViewController = vc      
    
            let textField = UITextField(frame: CGRect(x: vc.view.frame.midX, y: vc.view.frame.midY, width: 80, height: 20))
       
            //put layout constraints here
            textField.backgroundColor = .white
            textField.delegate = self
        
            vc.view.addSubview(textField)
            vc.view.bringSubviewToFront(textField)
        
            guard let _ = (scene as? UIWindowScene) else { return }
    }
    

    You can also present that controller from another or push to nav stack.

    Login or Signup to reply.
  5. Disadvantages:

    1. when creating instance of a controller will require you to create instance of its storyboard will increase development effort
    2. Mange more files of storyboard where single could have done the trick
    3. Memorise name of storyboards along with view controllers
    4. keeping them some where in constant will increase development effort and cost
    5. more files in project will look like mess in project
    6. Navigation to different viewControllers can be conflicting if not handled carefully can Cause application crashes too.

    Advantage

    1. Will load storyboard immidiately like xib files
      its the only advantage I can think of
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search