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

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

问题描述

我有一个应用程序,我想要一个背景"视图(视图控制器视图),最重要的是,多个 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.hCircle.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 的讨论,以了解传统 MVC 与 Cocoa 中 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天全站免登陆