如何按照MVC架构画一个圆

发布于 2024-12-07 03:56:52 字数 190 浏览 3 评论 0原文

我是 iOS 平台的新手,听说过 MVC 架构。 要绘制一个圆圈,我只需创建一个单独的 UIView 类并覆盖 drawRect: 即可做到这一点。

但现在我想使用 MVC 架构重建相同的项目。主要目的是将我的模型部分与视图和视图分开。控制器部分。这样我就可以扩展我的项目。

那么我该怎么做呢?

有申请样本可供参考吗?

I am new to iOS platform and i heard about MVC archtecture.
To draw a circle i just create a separate UIView class and override the drawRect: and able to do this.

But now i want to rebuild the same project using MVC architecture.The main aim is to separate my Model part from View & Controller part.So that i can extend my project.

So how can i do this?

Any sample application for reference?

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

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

发布评论

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

评论(2

陌上芳菲 2024-12-14 03:56:52

我不确定你想分开什么。绘制一个圆圈通常适合 MVC 架构的“视图”部分,这就是您已经完成的操作。例如,改变圆圈颜色的代码将位于“Controller”部分,在 iOS 上它是一个 UIViewController。如果您有一些存储有关圆的外观的信息(即大小、颜色等),则可以将其视为模型的一部分,并且可以存储在其自己的类中,由视图控制器拉入并传递给需要时查看。

这是一个可能有帮助的链接。

http://www.bit-101.com/blog/?p=1969

至于模型方面,Core Data 是一项可以提供帮助的技术。

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html#//apple_ref/doc/uid/TP40001075

I'm not sure what you want to separate. Drawing a circle would generally fit into the "View" part of the MVC architecture, which is what you have already done. Code that would, for instance, change the colour of the circle would live in the "Controller" part, which on iOS is a UIViewController. If you had something storing information on what the circle looked like (i.e. size, colour, etc.), that could be considered part of the model, and can be stored in its own class, pulled in by the view controller and passed to the view when it is neede.

Here is a link that may help.

http://www.bit-101.com/blog/?p=1969

As for the model side of things, Core Data is a technology that can help.

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html#//apple_ref/doc/uid/TP40001075

逆光飞翔i 2024-12-14 03:56:52

分离将是drawRect中的绘图代码以及模型中的大小和位置,控制器从模型获取圆属性并请求绘图并将属性传递到视图。该模型可能是另一个类或 API 提供的类,例如 NSDictionary。

如果有多个圆,则通过创建单独的模型,控制器可以发出多个绘制请求,每个模型圆一个。或者可能存在绘制圆的多个视图或不同的表示形式,例如一个视图中归属的圆的文本列表和另一视图中的图形圆。

许多模式看起来微不足道,在平凡的情况下并不是特别有用,但在现实世界的程序中却运行得很好。

The separation would be the drawing code in drawRect and the size and location in a model with a controller getting the circle attributes from the model and requesting the drawing and passing the attributes to the view. The model might be another class or an API supplied class such as an NSDictionary.

By creating a separate model if there were multiple circles the controller could make multiple draw requests, one per model circle. Or there could be multiple views that the circle(s) would be drawn in or different representations such as a text list of the circles attributed in one view and graphic circles in another.

Many patterns seem trivial and not particularly useful in the trivial case yet in a real-world program work very well.

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