使用MVC时是否需要创建单独的视图类?

发布于 2024-12-19 10:57:26 字数 336 浏览 2 评论 0原文

观看斯坦福 iTunesU CS193p 在线课程后,我对实现 MVC 设计的正确方法有一个简单的问题。

通常,当我创建应用程序时,我将视图设置为控制器的一部分,并向其中添加 UIButtons、UILabels 等。本质上,控制器和视图合二为一,除非您将 UIObjects 算作视图对象

在早期的 CS193p 示例之一中,导师在实现协议和委托之前将视图从控制器中分离出来作为一个单独的对象(UIView 的子类)控制器对象随后遵循的视图上的属性。

我只是对 CS193p 设计的实用性感到好奇,我可以理解分离视图更好地代表了 MVC 设计范式(特别是作为教学辅助),但对其在现实世界应用中的应用略有怀疑。

After watching the Stanford iTunesU CS193p online course I have quick question regarding the correct way to implement an MVC design.

Usually when I do an application I set my view up as part of the controller and add UIButtons, UILabels etc. to that. Essentially the controller and the view become one, unless you count the UIObjects as being the view objects

In one of the early CS193p examples the tutor splits the view off from the controller as a separate object (subclassing UIView), before implementing a protocol and delegate property on the view which the controller object then conforms to.

I am just curious about the practicality of the CS193p design, I can understand that splitting off the view better represents the MVC design paradigm (especially as a teaching aid) but slightly sceptical of its application in real world applications.

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

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

发布评论

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

评论(3

不甘平庸 2024-12-26 10:57:26

将“视图”视为 UIButton 和 UIImageView 等对象;可重用的组件不一定了解应用程序的其余部分。您的视图控制器是负责配置视图对象并管理其状态的对象。

有时您会想要子类化 UIView,主要是当您需要在 drawRect: 中进行自定义绘图时。不过,您不会子类化 UIView 来自定义行为,这就是视图控制器的用途。例如,您不会子类化 UITableView,而只是让表视图拥有自己的委托。相反,您可以将视图控制器设置为表视图的委托。

Think about the "view" as objects such as UIButton and UIImageView; re-usable components that don't necessarily know anything about the rest of your application. Your view controller is the object responsible for configuring the view objects and managing their state.

There are times when you'll want to subclass UIView, mostly when you need to do custom drawing in drawRect:. You wouldn't subclass UIView to customize behavior though, that's what your view controller is for. For example, you wouldn't subclass UITableView and just to make the table view its own delegate. Instead you'd make your view controller the table view's delegate.

倾城月光淡如水﹏ 2024-12-26 10:57:26

在 iOS 中,UIView 类实现绘图刷新和触摸处理程序,而不是视图控制器类。如果你需要drawRect:或touchesBegan:等,你将需要一个单独的UIView子类。

这部分可能是因为按钮和标签本身的行为方式也是如此。他们重新绘制自己,并委托处理的触摸。视图控制器不绘制它们的内容并跟踪 xy 位置。

With iOS, the UIView class implements drawing refresh and touch handlers, not the view controller class. If you need drawRect: or touchesBegan:, etc., you will need a separate UIView subclass.

This partially could be because this is how buttons and labels themselves behave as well. They redraw themselves, and delegate processed touches. The view controller doesn't draw their content and track xy locations.

梦在深巷 2024-12-26 10:57:26

这并不是绝对必要的。如果您使用 Interface Builder,那么您构建自定义视图(将 UI 设置代码从业务代码中分离出来)的大部分原因都会得到解决。但是,如果您想为您的视图构建自定义操作,或者设置一些您无法使用 IB 完全获得的属性,那么将您的视图拆分为一个单独的类是有意义的。

It's not strictly necessary. If you use Interface Builder, most of the reasons why you would build a custom view (breaking UI setup code out of your business code) are taken care of. However, if you want to build custom actions to your view, or setup some properties that you can't quite get at with IB, then it would make sense to split off your view into a separate class.

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