mvc
06 Sep 2014As I have been tutoring and helping out with questions. I have noticed that there’s a disconnect between Object Oriented programming and how to apply MVC to it. Here’s a small blurb on MVC and I’ll try to find the links to resources that have helped me out with understanding how MVC works.
Basics
Models are essentially a class that represents a type of data. Models are like a structure, a scaffold of sorts for data. Models strive to be abstract, good models tend to be great at dealing with various situations. Models are better off being lightweight, this is because models will be passed around a lot, so they are a convenient way to manage data.
Views can be described as both the objects to manage user interaction and the objects in the view designed to present the data to the user. A question that comes up frequently is why views are distinct from models: views typically have functionality that works alongside memory management. Since visual elements impact performance, models are distanced from views, especially in iOS where views tend to be temporal and managed by the UIKit framework.
Controllers help manage models and some controllers manage views and the user’s interactions with views. By giving the controllers the situational logic to handle models and views, it places the code that manages relationships in a convenient place rather than in two different files. If you placed them in models, it would bloat the model class with situational logic. This is not necessarily bad, but the best models strive to be as flexible as possible. Controllers are also nice because they are used to help set up the views and help with start up.
ViewControllers I’ve learned that View Controllers are very confusing to people new to programming. However once you understand them its easy to deduce how to build your own controllers to hold data or to hold network calls, core data requests, and etc.
In the Cocoa sphere of things the View Controller when you build a Single View Controller that is provided to you as boilerplate code by Apple inherits from UIViewController. UIViewController was designed by Apple to be the superclass for all controllers that manage what is presented to the user. Thus View Controllers manage UI objects and model objects that are presented to the user.
From this one can infer that a controller that manages network calls would hold onto the data received by the requests, the URLs to request information from, and message to send the data received to other controllers that would like to stay in the loop.
Next Time
Next week I’ll write a little bit on AppDelegate, it seems to scare newcomers away, but once you get to know it, you’ll start to love like me because it is a great way to simplify your code even further by leveraging the fact that it is a necessary singleton for an application to even run.