I’m learning cocoa (not ios) and I want to understand and implement MVC in all the code I create. I have learnt and understand the logic for this design and I am trying to use it in my apps (very small apps from lessons and tutorials etc..).
Most tutorials show examples of MVC in just 2 files. The Model and the appController together in one class/file (a sub class of NSObject) and then the view obviously in its own file (NSView).
But when I think of MVC, I always think of 3 classes in 3 files:
Class/file 1. The Model e.g. names, people, ages , arrays etc..
Class/file 2. The appController – the brain e.g. give person a name and age etc…
Class/file 3. The view e.g. show the person in a window.
So should the model always be in a separate file from the controller?
The other question is regarding communication – On the above example, how would the Model and appController share data ? Would the Model be a subclass of the appController (appController a subClass of NSObject) so both can share ivars ??
Thanks for reading this and I hope you can clear my confusion.
It is true that the Model is somewhat more abstract than the View and ViewController.
The Model may be a class but if it is a straightforward model, (say just a single value that the ViewController can get from some other ViewController) it may just be in the ViewController.
The key point is that the View never owns its own data, it just displays it.
Lecture 1 in CS193P has a lot of good illustrations and has a download of the lecture as a pdf here
Also, although a view should never own its own data, where that data is entirely derived from what is in the view (a box in the view for instance whose size is derived from the amount of data received), that box size could be worked out by the View when it is drawing itself.
Also, there is an example of the Calculator that this lecture refers to here.
I learned a great deal about the concepts in these places. I just struggle with syntax now.