MVC:将模型指针传递给视图? [英] MVC: passing model pointer to a view?

查看:56
本文介绍了MVC:将模型指针传递给视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的 iOS 应用程序,我正在尝试清理一些代码结构和实现.我想澄清我对 MVC 的理解并改进我的代码.

I have an iOS app working and am trying to clean up some of the code structure and implementation. I'd like to clarify my understanding of MVC and improve my code.

问题:将模型传递给 UIView 以便视图可以根据模型元素的状态呈现它是否合法?

Question: is it legit to pass a model to a UIView so the view can render it based on the states of the model elements?

我在下面概述了一个示例,但意识到它可能不清楚,阅读起来太乏味等.要点是:如果 UIView 没有更改模型中的任何值,它是否可以保留对该模型?(比方说总是要求它的委托返回一个指向模型的临时指针).

I outline an example below but realize it may not be clear, too tedious to read, etc. The gist is: if a UIView doesn't change any values in a model, is it ok that it retains a weak reference to the model? (say compared to always asking its delegate to return a temporary pointer to the model).

--感谢您的评论!

示例:想象一个 UIView,它代表一个 10 层楼的建筑,每层有 1 个 WINDOW.这个模型是一个 NSArray,包含 10 个自定义 WINDOW 对象的实例.每个 WINDOW 对象.有一个状态(灯开或关)和一个 CGRect 表示 WINDOW 在整个建筑视图 rect 中的位置.

Example: imagine a UIView which represents a 10 story BUILDING with 1 WINDOW on each floor. The MODEL for this is an NSArray containing 10 instances of custom WINDOW objs. Each WINDOW obj. has a state (light on or off) and a CGRect representing the position of the WINDOW within the overall building view rect.

BUILDING 实例的控制器定义了建筑视图(它的框架)和所有 WINDOW 对象的大小,包括 CGRects、它的状态等,创建了 NSArray 模型.然后我将此模型分配给 BUILDING 控制器的 UIView(但保留是 BUILDING 控制器的强大属性).

The controller for the BUILDING instance defines the size of the building view (it's frame) and all the WINDOW objs, including the CGRects,it's state etc., creating the NSArray MODEL. I then assign this MODEL to the UIView for the BUILDING controller (but retain is as a strong property of the BUILDING controller).

UIView 需要知道 WINDOW 和 CGRect 的状态才能在 drawRect 中绘制视图.

The UIView needs to know about the state of the WINDOW and the CGRect in order to draw the view in drawRect.

我想我可以单独存储 CGRects,因为它们不是抽象数据,但将它们全部打包到一个对象数组中似乎更容易.

I guess I could store the CGRects separately since they are not abstract data but it seemed easier to pack it all into one array of objects.

推荐答案

您走对了.但是视图没有理由对模型有弱"引用.对它实际显示的模型部分有一个强引用是合适的,除非您希望模型数据能够在显示时消失.那将是不寻常的.

You're on the right track. But there is no reason for the view to have a "weak" reference to the model. It is appropriate for it to have a strong reference to the piece of the model that it is actually displaying, unless you want the piece of model data to be able to vanish while it's being displayed. That would be unusual.

假设我有一个叫做 WindowPaneView 的东西,它显示一个 WindowPane(只是避免与 UIWindow 在这里混淆).控制器创建视图并将其传递给 WindowPane 的强引用并没有错.在许多情况下,这是一个非常好的设计.

Say I have this thing called WindowPaneView that displays a WindowPane (just avoiding any confusion with UIWindow here). There is nothing wrong with the controller creating the view and handing it a strong reference to the WindowPane. That is a very good design in many cases.

WindowPaneViewBuilding 发出请求以获得正确的窗口信息是错误的.控制器应该与 Building 对话,划分信息,并为每个 WindowPaneView 分配合适的 Window.

What would be wrong is for the WindowPaneView to make requests to the Building to get the correct window information. The controller should talk to the Building, divvy up the information, and hand each WindowPaneView its proper Window.

WindowPane 知道任何关于 WindowPaneView 的信息也是错误的.模型永远不能知道视图.当这种情况发生时,疯狂会迅速下降.但是视图肯定知道它们直接显示的模型的特定部分.只是没有了.

It would also be wrong for the WindowPane to know anything about the WindowPaneView. The model must never, ever, know about the views. Madness quickly descends when that happens. But views may certainly know about the specific piece of the model that they are directly displaying. Just no more.

这篇关于MVC:将模型指针传递给视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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