views:

55

answers:

1

So I have an MVC-application in Cocoa.
There are some custom views, a controller and a model. Of course, the views need to know some stuff, so they get their data from the controller. However, they do not use accessors in the controller, they use KVC with a keypath that calls right through to the model:

// In view.m
time = [timeSource valueForKeyPath:@"theModel.currentTime"];
// timeSource is a pseudo-delegate of the view that holds the controller

This simplifies things a great deal and technically, the views still don't know the model in person (that is, in pointer). But of course, they access it directly.

Is that a usual (or at least sensible) usage of KVC and MVC?
Or how would you implement this kind of communication?

+1  A: 

It seems awfully close to "knowing" the model to me. I don't know how most people do it, but I would be more likely to make properties on the controller. The reason I would avoid that, is that if you change the model, the views break. As I understand it, one of the main strengths of MVC is that if you change the model, you don't have to change the view.

Adrian Sarli
And then, in the controller you simply pass up the values from the model and override `keyPathsForValuesAffectingValueForKey:`, so changes in the model propagate back to the view.
BastiBechtold