MVC-视图可以继续引用其视图控制器并处理数据吗? [英] MVC - Is it OK for views to keep reference to their view controller and handle data?

查看:112
本文介绍了MVC-视图可以继续引用其视图控制器并处理数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Cocoa-Touch,但这是一个普遍的问题。

Working with Cocoa-Touch, but this is a general question.

视图可以保留对其视图控制器的引用并处理数据吗?我最近遇到了贯穿整个应用程序执行此操作的代码库-例如,有一个表视图控制器,并且其单元格对视图控制器的引用很弱(因此没有保留周期)。单元格通过

Is it OK for views to keep a references to their view controller and deal with data? I recently came across a code base that does this through out the app - For example, there's a table view controller and it's cells have a weak reference to the view controller (so no retain cycles). The cells present new view controller via

[self.parentVC presentViewController.....];

并以类似方式将其关闭。此外,它们还处理数据/网络操作中的更改。

and dismiss it in similar fashion. Also, they handle changes in data/network operations as well.

直到我看到这一点,我习惯为每个单元创建协议并通过它们与父视图控制器进行对话。使用直接引用视图控制器的方法摆脱了所有协议。

Until I saw this, I was accustomed to creating protocols for each cell and talking to the parent view controller via them. Using the approach of directly referencing view controller get rid of all the protocols.

在我看来,这确实很糟糕,但不确定是否常见。

To me this seems really bad but not sure if it's common.

推荐答案

即将到来的答案是关于 Apple处理模型视图控制器的方式,该方式与其他SDK完全不同(例如 Qt )。

The upcoming answer is regarding Apple way of handling the model view controller which is quite different than other SDKs (ex Qt).

这绝对是不好的做法。可以归结为一个问题,如果您认为这是一个好主意,那么为什么Apple尚未在 UIView 类中添加该引用?

This is definitely bad practice. And it can be sum up into one question, if you think this is a good idea, then why hasn't Apple already added that reference in the UIView class ?

如果您查看 Apple使用模型视图控制器,模型非常清晰:

If you look at how Apple uses the model view controller, the model is pretty clear:

视图与控制器通信以发出信号状态更改后,控制器会根据需要更新模型并相应地更新视图。您将永远不会找到苹果的文档或示例,因为您的视图将更新堆栈控制器。

The view communicates with the controller to signal a status change, the controller updates the model if needed and update the view accordingly. You will never find a piece of documentation or a sample for Apple where your view would update the stack controllers.

如果 UIView 子类开始保存对持有它的控制器的引用(即使它是一个弱引用),您也正在删除对其控制器的视图的抽象(该引用相反),因此强烈鼓励不良做法对于那些使用相同代码的人。

If a UIView subclass starts to hold reference to the controllers that's holding it (and even if it's a weak reference), you are removing the abstraction of the view towards its controller (the reference goes the other way), and therefore strongly encouraging bad practice for those working on the same code.

什么会阻止其他开发者这次在视图附加到的模型上添加引用?

What would prevent another developer to add a reference this time on the "Model" the view is attached to ?

现在,我并不是说这会将您的代码转换为意大利面条式代码(您仍然可以使用它来制作一个像样的应用程序),我是说这确实(真的!)是一种不好的做法。

Now I'm not saying this will turn your code into spaghetti code (you can still probably make a decent app out of it), I'm saying this is really (really!) bad practice.

希望这很清楚。

这篇关于MVC-视图可以继续引用其视图控制器并处理数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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