Search code examples
iosxcodestoryboardsubclassscene

Does OOP apply to Xcode storyboards?


I'm working on a flash card app. The user picks the question and answer categories separately. Some question categories are textual ("What is the capital of Brazil?", others are images ("This is the national flag of what country?").

The questions in a given round will either be always text or always images. Therefore, I have two separate scenes in my storyboards, one for each option.

90% of the scenes are the same: Question counter, Correct and Incorrect answers counters, 4 UIButtons for multiple choice answers, UIButton to return to main menu. Similarly, 90% of the code for each UIViewController subclass is also the same and so I will abstract it to a superclass.

But...can the same be done for the visual elements? Like a PowerPoint presentation that has the same header on each page.

I'd love to have two "layers" on my scenes....the first layer presents the common elements and is controlled by a single parent VC, the second layer presents the elements specific to each scene and is controlled by the corresponding VC.

Am I dreaming? Or is there something I haven't yet learned or figured out?

While writing this, I'm thinking that custom views might be the solution, but I can't get my head around it. Even with a custom view, I'd still have to make the connections to the common elements each time, won't I?

Any thoughts, hints, suggestions are more than welcome.


Solution

  • What you are describing is no problem at all! You can create a superclass, let's call it SuperVC (which itself is a subclass of UIViewController), add IBOutlets to it, then create subclasses of SuperVC, let's call them SubVC1 and SubVC2, and also create IBOutlets in those. Then, in your Storyboard you can connect the appropriate elements to all these IBOutlets, only make sure that you open the correct file in the Assistant Editor to do so (logically, if you connect IBOutlets from SuperVC you need to have SuperVC open in the Asisstant Editor, likewise for SubVC1 and SubVC2).

    By the way, you can do this with custom UIViews as well. Here again, you also have the option whether you want to create these in Interface Builder or in code... so really, there are no technical limitations on this issue :)