Search code examples
swiftoopdesign-patternsuiviewcontrollerreusability

swift reusability of ViewControllers


There are multiple view controllers in my current project that are used more than once in the app. My question is, how should I go about approaching reusing view controllers? So far I've thought of 3 possible ways:

  • Write the controller completely in code
  • Create the controller in the storyboard as usual, give it a storyboard id and call `instantiateViewControllerWithId:'
  • Create the controller in a nib file and load it up from nib.

My question becomes: What are the pros and cons of each method? Which one is cleaner in terms of maintenance and lastly which one would you, as a savvy/professional iOS app developper, favour?


Solution

  • All three approaches are same in terms of re-usability. Currently the most used approach is to create as most as possible in the storyboard using auto layout constraints. And then use instantiateViewControllerWithId if you want to reuse it. I would recommend you this also.

    Some people tend to do everthing programatically and sometimes it makes sense to do this. For example: sharing storyboards over version control with other developers is sometimes problematic.

    Using XIB for view controllers is outdated and generally not recommended anymore.