了解模型 - 视图 - 控制器 [英] Understanding Model-View-Controller

查看:86
本文介绍了了解模型 - 视图 - 控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我希望有一个背景视图(视图控制器视图),最重要的是,多个 UIView 将自己绘制为圆圈。我只是不明白如何在坚持MVC的同时实现所有这些......

I have an app where I want to have one "background" view (view controller view), and on top of that, multiple UIViews that draw themselves as circles. I just don't understand how to implement all of this while still adhering to MVC...

模型和视图应该是不同的。这是否意味着我通常需要一组用于视图的头文件和实现文件以及另一个(单独的)模型集 - 即使我的对象只是一个 Circle 类?或者,我会有一个 Circle.h Circle.m 设置,然后在头文件里面有两个界面块,一个用于模型,一个用于视图,以及两个实现块(同样,一个用于模型,一个用于视图)?

The model and the view should be different. Does this mean that I generally need one set of header and implementation files for the view and another (separate) set for the model — even if my object is just a Circle class? Or, would I have one Circle.h and Circle.m set, and then inside the header file have two interface blocks, one for the model and one for the view, and two implementation blocks (again, one for the model and one for the view)?

我见过一个很少有人建议使用视图控制器来处理 loadView 中的所有视图,并避免创建单独的自定义视图对象;这是否意味着我不应该为视图创建一组单独的 .h .m 文件?对我来说,后者在设计方面看起来更有条理。

I’ve seen a few people recommend using the view controller to handle all of the views in loadView, and to avoid creating separate custom view objects; does this mean that I shouldn’t create a separate set of .h and .m files for the view? The latter seems a lot more organized in terms of design to me.

另外,如果我有多个视图对象和多个模型对象由视图控制器处理,我将它们存储在控制器中的两个数组中 - 一个用于模型,另一个用于视图。对?我的问题是:当我在每个模型上使用键值观察时,对于每个模型对象,我将如何更改正确的相应视图对象(假设两个数组中的两个对象的索引都不相同)?

Also, if I were to have multiple view objects and multiple model objects handled by the view controller, I would store these in two arrays in the controller — one for models, and another for views. Right? My question is: When I use key-value observing on each model, how would I, for each model object, alter the correct corresponding view object (assuming that indexes are not the same for both objects in both arrays)?

我正在考虑使用指向视图内部模型对象的指针,以及从视图到模型的KVOing - 但是它不会通过控制器从而破坏MVC总之,对吗?

I was thinking about using a pointer to the model object inside of the view, and KVOing from the view to the model — but then it doesn’t go through the controller and thus breaks MVC altogether, right?

提前谢谢你。

推荐答案

MVC是一个大而广泛的想法。它更像是一种指导哲学,而不是一种特定的规则,并不总是以同样的方式实施。阅读 Apple对MVC的讨论,以了解Cocoa中传统MVC和MVC之间的区别。

MVC is a big, broad idea. It's more a guiding philosophy than a specific rule, and it's not always implemented the same way. Read Apple's discussion of MVC to appreciate the difference between traditional MVC and MVC in Cocoa.

很难说如何将MVC应用到你的应用程序因为你没有告诉我们应用程序应该做什么,而且它听起来不像是一个真实的应用程序。所以我会在这里尽力而为,并在此过程中做出一些假设。一个只在背景上的固定位置绘制一堆圆圈的应用程序并不是很有趣 - 它几乎可以是所有视图,几乎不需要任何控制器。因此,我们可以说圆圈都在不同的方向上移动,以不同的颜色绘制,并随着时间的推移而改变大小。现在您开始需要一个模型,以便您可以跟踪这些圆圈所代表的数据,以及一个控制器,将模型转换为可由视图表示的术语。

It's hard to say how to apply MVC to your app because you haven't told us what the app should do, and also it doesn't sound like a realistic application. So I'll do my best here and make some assumptions along the way. An app that just draws a bunch of circles in fixed locations on a background isn't very interesting -- it could be almost all views, barely any need for a controller at all. So lets say that the circles are all moving in different directions, are drawn in different colors, and change in size over time. Now you start to have a need for a model, so that you can keep track of the data that these circles represent, and a controller to translate the model into terms that can be represented by the views.

由于您专门询问了绘制圆圈,让我们从视图开始。在给定必要参数的情况下,拥有一个知道如何绘制圆的自定义视图似乎是个好主意:区域,颜色和位置。你可能会制作这些东西属性,并覆盖-drawRect:这样它就会以给定的颜色绘制给定区域的圆圈。

Since you asked specifically about drawing circles, lets start with the view. It seems like a good idea to have a custom view that knows how to draw a circle given the necessary parameters: area, color, and position. You'd probably make these things properties, and override -drawRect: such that it draws a circle of the given area in the given color.

我们不知道是什么这些圈子代表了,但如果它们不代表什么就没那么有趣,所以让我们假设应用程序的工作是帮助我们比较公司。我们有关于收入,市值,员工数量,信用评级,名称,股票代码等的数据。您可以创建一个自定义对象来存储每个公司的所有数据,或者您可以将它们全部放在字典中。我们的模型是一组这些自定义对象或词典。

We don't know what these circles represent, but it's not much fun if they don't represent something, so let's postulate that the app's job is to help us compare corporations. We have data on revenue, market capitalization, number of employees, credit rating, name, ticker, etc. You could create a custom object to store all the data for each corporation, or you could put it all in a dictionary. Our model is a set of these custom objects or dictionaries.

请注意,圆圈视图对公司一无所知,模型对圆圈一无所知。这是一件好事。它也是控制器的用武之地。控制器是您使用视图直观地表达模型的代码。它还解释视图中的事件,并根据需要更新模型。因此,我们的控制器知道公司的细节以及圆形视图的属性。它为模型中的每个公司创建一个圆形视图。我希望圆圈区域对应于公司的市值,表示收入的垂直位置和表示员工数量的水平位置。我们将根据公司的信用评级指定颜色。当然,控制器应该跟踪所有圆形视图以及在圆形视图和公司之间进行映射的某种方式。

Notice that the circle views don't know anything about corporations, and the model doesn't know anything about circles. This is A Good Thing. It's also where the controller comes in. The controller is where you put the code that expresses the model visually using the views. It also interprets events from the views, updating the model as necessary. So, our controller knows both about the particulars of the corporations, and the properties of the circle view. It creates a circle view for each corporation in the model. I want the area of a circle to correspond to the corporation's market cap, the vertical position to represent the revenue, and the horizontal position to indicate number of employees. We'll assign a color based on the corporation's credit rating. The controller should, of course, keep track of all the circle views and some way to map between circle views and corporations.

现在你有了一些东西。它仍然是一个非常简单的应用程序,但你有一个有用的图表,比较几个方面的公司。让我们改进它。

Now you've got something. It's still a pretty simple application, but you've got a useful chart comparing corporations in several dimensions. Let's improve it.

首先,很难知道哪个圈子代表哪个公司。如果圆形视图可以选择显示一些文本,那将是很好的。让我们添加标题和副标题属性,并修改-drawRect:分别在圆圈的上方和下方绘制这些字符串。我们还将更改控制器,以便点击圆圈或将圆圈的标题和副标题设置为公司的名称和股票代码,或者如果之前已设置,则清除它们。

First, it's hard to know which circle represents which corporation. It would be nice if a circle view could optionally display some text. Let's add title and subtitle properties, and modify -drawRect: to draw these strings above and below the circle, respectively. We'll also change the controller so that a tap or click on a circle sets the title and subtitle of that circle to it's corporation's name and ticker symbol, or clears them if they were previously set.

其次,在某个时刻对公司进行比较是很好的,但如果我们能够随着时间的推移显示变化,那就更有趣了。让我们改变模型,包括收入,市值,员工和评级的历史数据。我们可以更新控制器,以便它可以使用历史数据为圆圈设置动画。

Second, it's nice to compare corporations at a moment in time, but more interesting if we can show changes over time. Let's change the model to include historical data for revenue, market cap, employees, and rating. We can update the controller so that it can uses the historical data to animate the circles.

第一个变化与我们如何在屏幕上绘制信息有关,而不是根本不需要对模型进行任何修改。第二个变化是关于我们必须使用哪些数据,并且根本不需要对视图进行任何更改。您可以轻松地重复使用圆形视图来表示其他类型的数据,或者甚至可能成为空气曲棍球比赛中的冰球。这只是一个彩色圆圈。您可以在另一个处理同类数据的应用程序中重复使用该模型。

The first change related to how we draw information on the screen, and didn't require any modification to the model at all. The second change was about what data we have to work with, and didn't require any change to the view at all. You could easily re-use the circle view to represent some other kind of data, or maybe even to be the puck in an air hockey game. It's just a colored circle. And you could re-use the model in another app that deals with the same kind of data.

我确信在这个非常冗长的解释中的假设应用 - 示例与您自己的应用程序几乎没有相似之处,但也许它有助于解释为什么MVC有用并告知您自己的应用程序的结构。祝你好运。

I'm sure the hypothetical application in this very long-winded explanation-by-example bears roughly zero resemblance to your own application, but perhaps it'll help to explain why MVC is useful and inform the structure of your own application. Good luck.

这篇关于了解模型 - 视图 - 控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆