Question

I'm working on an app that has several functions (selected via tableview) that all pretty much do the same thing, similar layout, similar buttons, similar input fields, etc. But different enough where I believe it justifies their own class. Each will display their own type of data and have different input fields, but all of the fields and layout will look the same, or at least have the same theme. Plus having separate classes would allow me to customize how each is laid out and the data it will display.

I'd like to have some advice on the best way to approach this. Here are the ideas I've come up with:

Idea #1) Create one Scene for each function, along with a separate segue for each (could be 10-20 Scenes total). A BaseViewController would be created, along with a ViewController class for each function that would subclass BaseViewController for some code reuse.

Idea #2) Similar to Idea #2 in that a BaseViewController would be created and classes for each function. However, instead of having one scene for each function in the Storyboard, there would only be one Scene and one Segue to that Scene. Before loading the scene, the Class for the Scene would be set to selected function's class. Problem is I have no idea how to do this or if this is even possible, I've searched around and haven't found any examples.

I like Idea #2 because I don't have to create a new scene for every function, thus it's easier to create additional functions. But as I mentioned, not sure if this is even possible or if there's better approach.

Was it helpful?

Solution

Your question is titled "Reusing controller classes...", but it's not clear what you want to reuse:

...all pretty much do the same thing, similar layout, similar buttons, similar input fields, etc. But different enough where I believe it justifies their own class.

And then:

Each will display their own type of data and have different input fields, but all of the fields and layout will look the same, or at least have the same theme. Plus having separate classes would allow me to customize how each is laid out and the data it will display.

So you've ruled out reusing your view controller class, and it sounds like you also can't reuse the view layout.

My advice would be to think carefully about what, exactly, these several view controllers and layouts have in common. Once you've determined that, you have several options:

  • Create a common base class for the view controllers. Maybe they all use the same data model, and they all need to provide the same set of methods to interface with the model. Abstracting that out into a separate view controller subclass, and then deriving your view controllers from that base class could pay off.

  • Try delegation. Cocoa and Cocoa Touch contain many excellent examples of isolating things that are hard to reuse in a separate object. It may be that you can construct a single reusable view controller class that relies on a delegate to adjust its behavior.

  • Use a child view controller. If each of the scenes in question have a common part that's logically separate from the rest of the scene you could create a view controller to manage just that part, and then have the controller from each scene incorporate it as a child view controller.

  • Use a .xib file. If it's just the view hierarchy and the way the views are positioned that you want to reuse, consider using a .xib file to set up a single view hierarchy. Each view controller could then load that .xib to create its own copy of that view hierarchy.

Also, don't try to reuse things that shouldn't be reused. We read all the time about the benefits of reuse, but you only get those benefits if you reuse things appropriately. Rather than trying to design the reusable part up front, it may help to write the view controllers separately and then see where it makes sense to refactor your code into a common base class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top