0

In Cocoa MVC adoption, View knows nothing about the Model, which illustrated in this diagram:

Cocoa MVC interaction diagram

But consider this example:
I have Item class in my model and I want visual representation for it. Most obvious for me is ItemView class, which is initialized with Item.
So, in this way, I'm breaking Cocoa MVC rules and feeling uncomfortable with it. But, I'm feeling uncomfortable also not having class like ItemView. What is the most practical solution?

Alexander
  • 780
  • 1
  • 8
  • 22

1 Answers1

0

If you are really concerned about MVC, what about defining a method in your controller like:

- (UIView*)itemViewForItem:(Item*)item;

which is responsible for creating and "populating" your ItemView?

You main controller class would then act as a controller both for your main view and all ItemViews you have got.

Another approach would be giving each ItemView its own ItemViewController. This is perfectly fine and if your controller/view is of any complexity, IMO, also the best approach. The drawback with this is that dealing with controllers container is supported only on iOS>5.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • If I have not only ItemView, but also EmployeeView and CustomerView on one screen - controller becomes god object, which, in my opinion, is worse than breaking Cocoa MVC rules. – Alexander Jan 21 '13 at 18:32
  • Yeah, that is the case for separate controllers: ItemViewControllers, EmployeeViewController, etc. If you want to follow MVC, that is the way. On the other hand, I understand you are using custom views: think that you could be using standard `UIView` and "custom" controllers to the same effect. – sergio Jan 21 '13 at 18:37
  • btw, I am not saying that your design is bad. I think there are cases when breaking a little the rules is ok. I have been just trying to explain how IMO you could deal with MVC without breaking any rules (and don't forget that this has definitely a drawback on iOS<5, so your solution might be better on that account). – sergio Jan 21 '13 at 18:39
  • I think we should forgot iOS<5 time. Thank you, I will follow your advice about nested ViewControllers in future. – Alexander Jan 21 '13 at 19:01