将手势识别器/操作方法附加到视图会违反模型视图控制器吗?

发布于 2024-12-12 03:06:33 字数 163 浏览 0 评论 0原文

想问问大家对此的看法。我有一个 uiimageview 子类,在它的初始化程序中,我向 imageview 添加了一些手势识别器,并且还在类中包含了手势识别器委托方法。我的问题是,这是否违反模型视图控制器?所有与控制视图有关的代码都应该在视图控制器中吗?与将按钮操作方法放入视图中相同。无论如何,有兴趣听听意见。

Wanted to ask peoples opinion on this. I have a uiimageview subclass and in it's initializer I add some gesture recognizers to the imageview, and I also include the gesture recognizer delegate methods within class. My question is, does this violate model view controller? Should all the code that has to do with controlling a view be in a view controller? Same with say, putting a button action method in a view. Anyhow, interested in hearing opinions.

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

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

发布评论

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

评论(3

入画浅相思 2024-12-19 03:06:33

从某种意义上说,是的,这违反了 MVC 模式。正如您所说,视图不应该与如何控制它有任何关系,最好的习惯是将此类代码分组到应用程序的另一部分。

而且,OOP 的一大优点是能够重用类。这样的视图可以毫无问题地重用,因为所有控制代码(特定于应用程序)都不在其中。
如果您在视图中包含委托方法,您将无法重用它,或者每次都必须更改委托方法!


但规则适用于一般情况。看一下 Mac OS X 的 Cocoa 框架:您已经获得了 cocoa 绑定,它允许您直接更改视图内容以响应模型中的数据更改。这在某种程度上也违反了 MVC 模式。

另一个示例,请参阅 UIViewController。它一定是一个控制器,但它与视图的联系如此紧密,以至于问题仍然存在。视图与视图之间的分离控制器并不像 MVC 所希望的那样明确。


总而言之,我想说遵循模式(只要它们符合您的需求)以及使用给定框架遵循它的方式是一个好习惯。但在某些情况下,如果你明确说明,创建一些捷径并打破规则可能会很有趣。

In one sense, yes, this violate the MVC pattern. As you say, the view shouldn't have anything to do with how to control it, it's a better habit to group such code in another part of the application.

Moreover, one of the great advantages of OOP is the ability to reuse classes. Views as such can be reused without any problem, as all the controlling code — specific to the application — is not in them.
If you include the delegate methods in your view, you won't be able to reuse it, or you would have to change the delegate method each time!


But the rules are for the general case. Take a look at the Cocoa framework for Mac OS X : You've got the cocoa bindings which permit you to directly change a view content in response to a data change in your model. That does also violate the MVC pattern in some way.

One other example, see UIViewController. It must be a controller but it is so closely attached to a view that the question persists. The separation between view & controller is not as explicit as the MVC would like it.


To conclude, I would say that it's a good habit to follow the pattern (as long as they match your needs) and the way you can follow it using a given framework. But there's some case, if you make it explicit, where it could be interesting to create some shortcut and break the rules.

同展鸳鸯锦 2024-12-19 03:06:33

如果该操作只会影响视图的外观,那么这似乎是一个非常好的主意。例如,将平移手势识别器附加到视图以拖动它似乎是一个好主意。

但是,如果它会在您的应用程序中触发更广泛的影响,那么它可能不是正确的方法。

Seems like a perfectly good idea, if the action will only affect the appearance of the view. For example, attaching a pan gesture recognizer to a view to drag it around seems like a good idea.

However if it will trigger more wide-ranging effects in your application it's probably not the right approach.

雨落□心尘 2024-12-19 03:06:33

来自 Apple 开发人员核心能力文档 (https:// /developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html):

视图对象是应用程序中用户可以看到的对象。一个
视图对象知道如何绘制自身并可以响应用户操作。

就我个人而言,我将这解释为可以在视图类中编写手势识别器,只要手势是为了允许视图“响应用户操作”。

我认为 Apple API 中也有一些先例 - 因为一些 UIView 后代(例如 UIButton)可以识别手势。

From the Apple developer Core Competencies doc (https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html):

A view object is an object in an application that users can see. A
view object knows how to draw itself and can respond to user actions.

Personally I would interpret this to mean its OK to write gesture recognisers within a view class, provided the gesture is for allowing the view to "respond to user actions".

There is also some precedent in the Apple APIs I think - since some UIView descendants (UIButton for example) can recognise gestures.

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