Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

iOS design pattern: multiple child views in the same view controller

Hi, I’m trying to understand what must be a common ios design pattern, as applied in Swift. Basically, to collect a sort of user defined hierarchy of information in which the user can add sets of information / text fields at specific branches or not. To allow, for example, none or any number of addresses, phone numbers, email addresses. Visually, the user should see various sets of labels matching an instantiated class, strut or protocol (I’m not sure which) and the option to press a + button (not bar or tab) to show another set of the same labels on the same scrollable screen. Not only should it be possible to add the pre-defined sets /views / containers as needed, but then all except one containers should disappear, and then all containers should be shown again with all data before being finalized / saved / whatevered.

I was trying customizing a collection of a few Container Views inside other Container Views, but then I read in the apple docs that “If the new child view controller is already the child of a container view controller, it is removed from that container before being added.” Does this mean that it is not possible to have multiple instances of the same children on the same page?

I'd also like to know how data is managed at the base of the main container / view. Where data goes when it's view disappears, and how it reappears again.

2 Answers

multiple child views in the same view controller is not a design pattern. A common design pattern on the other hand, is MVC (model view controller).

A short summary of the MVC categories:

The model classes do the “real work”, the view classes display the results and take user input, the controller classes are the glue between the model and the views.

The model should know very little about the app context – it should be able to run on a different device or even a different OS with little change. If you should rewrite your app’s UI, you should not need to touch the model at all.

The views should be quite dumb. They should not care about data storage, application state and other subtleties. They should get a piece of data to display or relay user input to a controller, nothing more.

The controller calls the model to do the real work and then displays the results using the views. Or takes user input from the views and updates the model accordingly. It knows about application state and probably also about data storage, but shouldn’t know too much about the problem domain (that’s model’s job) or data display and input handling (view’s job).

Hope this was helpful.

Hello Thomas, thanks for replying. The MVC design pattern, as you describe, is fairly clear to me.

What is not clear is how to nest views or view controllers inside each other; and how to create multiple instances of the same model within each view so that data is passed back to some root controller level.

For example, in a single page app collecting data about a, b and c, the user may enter data for the variables a, b and c, but also may create another instance of b or c where different values can then be entered on the same screen below the first b or c. It should also be possible to then hide all but the 'a' field where more data can be entered; then re-show just b with a; and finally save all variables where data was entered.

To put it another way: the user is initially shown fields to enter their name, address, and phone number. The user has 3 addresses and 4 phone numbers. By pressing a button beneath the address or phone number field, an additional label and text field is created to enter the additional information. Pressing another button hides everything but their name; another button shows only their name and all addresses; and a final button saves all data entered.

"the user is initially shown fields to enter their name, address," - Ok. Here is one view.

"By pressing a button beneath the address or phone number field, an additional label and text field is created to enter the additional information. " - This is one or more reusable views. Alternatively, one ore more reusable subviews within that first/main view.

"final button saves all data entered." - Here, is where your view tell your controller that new information is added. The controller in turn lets the model now, so it can update it's data.

How can you can you let the view (or one of it's subviews) talk to the controller, you ask? In those views, set up a protocol/delegate.

So basically, this is very much MVC related.

Hope I don't misunderstand your question.

Maybe Amit Bijlani has some useful input as well.

Thanks Thomas! Yes, yes, yes - that sounds right. Leaving the model question aside for a moment, what exactly are those reusable subviews? Are they containers inserted into a view controller?

In its most simple form, if I create a textField in a root container (custom class MyContainer) with a button below the container, what code should be in my button's IBAction to simply create another duplicate container, and another, and another.

If it doesn't make it too complicated, where do I then set up a protocol / delegate to capture all data if the sub-views are hidden? No matter how much I read on this topic, I find it impregnable especially when trying to apply in Swift. Could you please give me the basic code units and locations in Swift?

Incidentally is each subview an instance of the custom class MyContainer? If so, how do I uniquely name each instance? Or, can these optional values be collected within a single dictionary or array?