设置简单文本标签的文本是否违背MVC?

发布于 2024-12-27 05:01:47 字数 457 浏览 1 评论 0原文

在 MVC 中,视图不应该保存它的数据。不过我知道在 Objective-c 中你会这样做:[textField setString:@"hello"];,然后该字符串将由文本字段保留。这同样适用于文本字段的字体和文本颜色等。

但是 UITableView 使用数据源向控制器询问其数据,然后由控制器重新加载表视图。但它本身也存储一些数据,例如背景颜色。

我可以理解 UITextView 不使用数据源的原因,如果每个属性都必须是一个方法,代码会变得更加冗长。但是为什么在某些情况下使用数据源而不是其他情况,为什么不只设置一个 UITableViewCells 数组(我知道这意味着单元格无法重用,因此它会使用更多内存,但还有其他设计原因),对于UITableView要显示?

创建自己的对象时,如何知道何时在视图中存储少量通用数据(例如,文本视图显示的字符串只能是字符串,但任何字符串本身都可以是任何内容),或者使用数据源?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2025-01-03 05:01:47

MVC 是一种模式,而不是法令。让视图完成工作。一些耦合即将发生。遵循模式的指导方针,并使其适应开发人员和组织的风格和愿望。

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.

海未深 2025-01-03 05:01:47

我不熟悉objective-c的mvc框架,但我想我理解这个问题。

基本上,您不希望视图对数据源后端执行任何操作,即与访问数据库的管道有关的任何操作。

但视图可以访问并使用数据本身。这就是MVC的M部分。该模型被传递。视图知道如何显示它。控制器知道如何对其执行业务逻辑(包括与数据访问层等后端系统交互)。

在数据网格的情况下,它必须访问后端才能获取数据,因此它必须依赖于控制器。

理想情况下,视图只知道与显示相关的信息(例如背景颜色)。整个想法是关注点分离。你希望视图只处理它的一部分事情,就像控制器一样。然后您可以彼此独立地修改它们。

至于数据源的细节(相对于数组),网格往往很复杂。也许这是处理分页或其他细节。在这种情况下,我不认为它是层的分离(因为数组可以很容易地成为模型),而是处理更多的功能。

我不确定您在视图中重新“存储”少量数据是什么意思。视图应该倾向于处理“视图内容”。

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文