Does setting the text of a simple text label go against MVC?

147 views Asked by At

In MVC the View shouldn't hold it's data. However I know in Objective-c you do: [textField setString:@"hello"];, that string is then retained by the text field. The same applies for the textField's font and text colour, etc.

However a UITableView uses a datasource to ask a controller for it's data, it's then up to the controller to reload the table view. But it also stores some data itself, like background colour.

I can understand a reason as to why a UITextView doesn't use a data source the code would become much more lengthy, if every property had to be a method. But why use a data source in some cases and not others, why not just set an array of UITableViewCells (I know that this means cells could not be reused and so it would use more memory, but what other design reason is there), for the UITableView to display?

And when creating you own objects how do you know when to just store a small amount of generic data (e.g. the string a textview displays can only be a string, but any the string itself can be anything)in a view, or use a datasource?

2

There are 2 answers

1
mtyson On

I'm not familiar with objective-c's mvc framework, but I think I understand the question.

Basically, you don't want the view doing anything with the datasource backend, that is, anything having to do with the plumbing of accessing the DB.

But its ok for the view to have access and use the data itself. That is the M part of MVC. The model gets passed around. The view knows how to display it. The controller knows how to do the business logic to it (including interacting with backend systems like the data access layer).

In the case of data grid, it has to hit the backend to get the data, so it has to rely on the controller.

Ideally, the view knows only about display related information (like the background color). The whole idea being separation of concerns. You want the view to handle just its part of things, like-wise the controller. Then you can modify them independently of each-other.

As for the specifics of the datasource (versus an array), grids tend to be complex. Maybe that is handling paging or other niceties. In this case, I don't think its so much the separation of layers (since an array could just as easily be the model), but handling more functionality.

I'm not sure what you mean re 'storing' small amounts of data in the view. The view should tend to deal with 'view stuff'.

0
jamesTheProgrammer On

MVC is a pattern, not an edict. Let the view do the work. Some coupling is just going to happen. Follow the guidelines of the pattern, and bend it to the style and desires of your developers and organization.