Julian
Julian

Reputation: 98

iOS: How can I reduce Storyboard & Code duplication?

I am currently developing a Scheduling app for fun, and in the App I want to have templates for a given day that can be loaded in, this will reduce creating each day from scratch, if there are specific things someone does daily.

This is currently what my Storyboard looks like with the App Screen

I want to add a UIButton which will change the state of the App into "Template" mode. From there I would like to be able to add tasks as I already do, and have them save to a JSON file. Users will also be able to "import" their template to the current day. I know how to do all that, but the thing I'm curious about is how would I go about creating this Template "state". Would it make more sense to create two duplicate Storyboard ViewControllers with some duplicate controller code? Or would that be considered bad practice? How could I switch between this "template state" and "normal schedule state"?

The layout of the Template state and the normal schedule will have the exact same UI Elements in the exact same place. I'm pretty much asking for the best way to tackle this, be that a specific design pattern or something along those lines.

Thank you!

Upvotes: 1

Views: 193

Answers (1)

Julian Silvestri
Julian Silvestri

Reputation: 2037

In my opinion I would create a separate storyboard for your "template" it is not bad practice to have multiple storyboards so long as they are not redundant.

You can also easily use your existing storyboard with a UIButton that will change the "state" of your scheduling view. There is nothing wrong with that and many will suggest to do so this way.

A few things to keep in mind:

-Using the same storyboard for multiple purposes other than what it is intended is not necessarily the best approach. It can lead to confusion especially if you are not keep your code and functions clean and tidy.

-Using a separate storyboard for this would be easier, as it would be straightforward as to what each storyboard is intended to do. You will get less confused and be more efficient with your time. However, this means you will have to manage an entirely new viewcontroller and storyboard on top of your stack. This means more work and a larger codebase. Properly dismissing the viewController and managing your stack is key.

A BETTER WAY

For your view, you can use an XIB file. An xib file will allow you to reuse the same view with ease. Dynamically changing the content and functionality is an XIB files intended purpose.

Here are several links for you information gathering. https://medium.com/better-programming/swift-3-creating-a-custom-view-from-a-xib-ecdfe5b3a960

https://medium.com/@tjcarney89/whats-a-xib-and-why-would-i-ever-use-one-58d608cd5e9b

https://mobilejazz.com/blog/storyboards-xibs-best-practices/

NOTE Using and creating an .xib is not super simple and is not straightforward. If you are new to ios development i would shy away from this for now until you are more comfortable with development. You may find yourself spending more time setting this up then it would using your existing storyboard or a new one

Upvotes: 1

Related Questions