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 Build a Simple iPhone App (iOS7) Refactoring into a Model Creating a Custom Class

Separate view layer? I don't get it.

I'm confused as to how the view layer is separate from the view controller. I realize that, in this case, the storyboard makes up the view layer, but when you walked through the example of creating the button programmatically, you added that code in the view controlled class. How does this keep the view separate from the view controller?

If you wanted to code the view programmatically instead of using the drag-and-drop storyboard, would you create a view class? Not sure how this would look.

Any thoughts?

Is there a way to see the code that underpins the storyboard?

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

A storyboard is an XML file that contains a list of views and controls along with the size and positioning. The SDK parses the XML and draws the relevant views on screen. You can either take advantage of interface builder or do it all on your own in code.

Yes, clearly. My question is: in the MVC model where does the "view code" go. In your example in the video you put it in the view controller class.

What's best practice?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

It all depends. If all you are doing is adding a couple of controls then adding it to the view controller is fine. If you are creating a complex view that could be reused then you would create your own custom class that would subclass UIView and layout all its subviews. Back in your view controller you would add that custom view as a subview in your view controller. You are not violating MVC by adding views in your view controller. You will certainly be violating MVC if you are creating an entire view layout in your view controller. Hope that makes sense.