了解 iOS 应用程序中使用的 MVC 模式

发布于 2024-11-02 04:55:35 字数 502 浏览 2 评论 0原文

我已阅读Apple的MVC 文章,对各种事情感到困惑。首先,Apple 在几乎所有示例应用程序中都使用了视图和控制器的组合,这很好,我喜欢它,但它们在本文中自相矛盾,因为他们说视图不应该依赖控制器等。

我的主要问题是有人有吗Apple 示例 iOS 项目之一的链接,这是 MVC 模式的一个很好的示例 - 具有数据检索等功能,因为我不完全理解该模式的模型部分。

我不明白“域对象”和模型对象之间的区别。例如,如果我想检索订单列表,这将在模型类 Orders 中发生。然后我是否会拥有另一个具有 OrderDate、OrderNumber 等属性的 Order 类,或者它如何工作?

I have read Apple's MVC article and am confused about various things. Firstly Apple uses a combination of the View and the Controller in almost all its sample applications which is fine and I like it but they contradict themselves in this article because they said that View's should not rely on Controllers etc.

My main question is does anyone have a link to one of Apple's sample iOS projects which is a good example of the MVC pattern - with data retrieval etc because I don't fully understand the Model part of the pattern.

I don't understand the difference between a 'domain object' and a model object. For example if I wanted to retrieve a list of orders this would happen in a model class Orders. Would I then have another class Order which has properties such as OrderDate, OrderNumber etc or how would this work?

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

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

发布评论

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

评论(5

病毒体 2024-11-09 04:55:35

示例代码演示了加载和显示 UITableView 的多阶段方法。我认为深入研究真的很有趣。它将展示 MVC 的工作原理。

This sample code demonstrates a multi-stage approach to loading and displaying a UITableView. I think it's really interesting to dive in. It will show MVC in the works.

雨后咖啡店 2024-11-09 04:55:35

以下是模型-视图-控制器(也称为 MVC)模式如何映射到应用程序的主要部分:

模型 → 数据

视图 → 用户界面

控制器 → 核心逻辑,

这通过示例代码进行了完整解释

http://www.hollance.com/2011/04/ Making-your-classes-talk-to-each-other-part-1/

在此处输入图像描述

Here’s how the Model-View-Controller (also known as MVC) pattern maps to the main parts of your App:

Model → Data

View → User Interface

Controller → Core Logic

this explain fully with sample code

http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/

enter image description here

可可 2024-11-09 04:55:35

我相信以下代码将帮助您了解如何在 iOS 应用程序中使用 MVC,因为它的描述如下:

“MVCNetworking 是一个示例,展示了如何创建网络
使用模型-视图-控制器设计模式的应用程序。
具体来说,它通过获取图库的 XML 来显示照片库
来自网络服务器的描述、缩略图和照片,并使用 Core
用于在本地缓存此信息的数据。”

http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010443

I believe that the following code will help you understand how to work with MVC in iOS application because its description says:

"MVCNetworking is a sample that shows how to create a network
application using the Model-View-Controller design pattern.
Specifically, it displays a photo gallery by getting the gallery's XML
description, thumbnails and photos from a web server, and uses Core
Data to cache this information locally."

http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010443

冬天旳寂寞 2024-11-09 04:55:35
  • 模型是应用程序的大脑。它的作用是
    计算并为自己创造一个可以生存的虚拟世界
    没有视图和控制器。换句话说,想一个模型
    作为应用程序的虚拟副本,没有面孔!

  • 视图是用户与您的交互的窗口
    应用。它大多数时候显示模型内部的内容,
    但除此之外,它还接受用户的交互。任何
    用户和您的应用程序之间的交互被发送到视图,
    然后可以由视图控制器捕获并发送到
    模型。

  • iOS编程中的

    控制器通常指的是视图控制器。将视图控制器视为模型和您的模型之间的桥梁
    意见。他们解释一侧发生的事情(用户
    在视图侧,或模型提供的信息)和
    使用该信息根据需要更改另一端。

  • The model is the brain of the application. It does the
    calculations and creates a virtual world for itself that can live
    without the views and controllers. In other words, think of a model
    as a virtual copy of your application, without a face!

  • A view is the window through which your users interact with your
    application. It displays what’s inside the model most of the time,
    but in addition to that, it accepts users’ interactions. Any
    interaction between the user and your application is sent to a view,
    which then can be captured by a view controller and sent to the
    model.

  • Controllers in iOS programming usually refer to view controllers. Think of view controllers as a bridge between the model and your
    views. They interpret what is happening on one side (what the user
    does on the view side, or the information provided by the model) and
    use that information to alter the other side as needed.

挽清梦 2024-11-09 04:55:35

这是迄今为止我遇到的最好但简单的解释(取自 RayWenderlich)

“MVC 背后的想法是
- 视图应该只关心它们的呈现方式如何呈现,
- 模型应该只关心他们的数据,
- 控制器应该努力将两者结合起来,而不必了解太多他们的内部结构。”

This is by far the best, yet simple explanation I've come across(taken from RayWenderlich)

"The idea behind MVC is that
- VIEWS should only care about how they are presented HOW THEY ARE PRESENTED,
- MODELS should only care about their DATA,
- and CONTROLLERS should work to MARRY the two WITHOUT necessarily knowing too much about their internal structure."

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