iPhone MVC。模型的问题

发布于 2024-09-18 02:08:30 字数 537 浏览 6 评论 0原文

我是编程新手,特别是 iPhone 应用程序编程。在阅读了大量有关 MVC 的内容后,我决定在一个小型应用程序中尝试一下。根据我的理解,MVC 的工作原理是这样的:

模型:数据、操作数据、检索数据。 ViewController:格式化模型中的数据(NSDate 到特定样式)等。 View:实际的gui。

如果这确实是基本 MVC 理论的正确表述,那么我的困惑在于数据如何在模型、VC 和视图之间传递。示例:如果我调用 twitter 并获取模型中的数据,我如何(正确地)将此信息传递给 VC 进行进一步的工作。我知道VC和View之间主要使用IBOutlets。模型是我真正的问题。

在我的上一个应用程序中,我在应用程序委托中创建了一个 NSString 变量,以便我可以从任何类访问该数据。然而,我读到,当应用程序变得复杂时,这不是最好的方法,因为委托负责启动、结束应用程序,而不是保存数据。

我读过委托方法、单例方法、NSNotification(我用它来调用其他类中的方法)。问题是我真的不明白如何使用这些技术将数据从模型传递到其他视图。

如果我的问题不清楚,请告诉我。

I'm new to programming, iphone application programming in specific. After reading a bunch about MVC I decided to give it a try in a small application. As to my understanding, MVC works like this:

Model: data, manipulating data, retrieving data.
ViewController: formats data from the model (NSDate to specific style), etc.
View: the actual gui.

If this is indeed a correct formulation of basic MVC theory, my confusion lies in how data is passed between the model, VC, and view. Example: if I make calls to twitter and get the data in the model, how do I (correctly) pass this information to the VC for further work. I know that between the VC and View one mostly uses IBOutlets. The Model is my real problem.

In my last app I made an NSString variable in the app delegate so I could access that data from any class. However, I read that this is not the best approach when the app becomes complex because the delegate is in charge of starting, ending the app, not holding data.

I've read of delegation methods, singleton's, NSNotification (which I've used to call methods in other classes). The problem is that I don't really understand how to use these techniques to pass data from the model to other views.

Please let me know if my question is unclear.

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

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

发布评论

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

评论(2

第七度阳光i 2024-09-25 02:08:30

如果考虑可重用性,可以再次重用的主要组件是模型对象和视图对象。它们可以移动到不同的应用程序并仍然可以正常使用。您的视图控制器是您的应用程序真正特定的部分,也是大多数应用程序逻辑所在的位置。

因此,在您的示例中,您可能有一个 twitter 对象,它可能存储来自用户的信息和推文。您将在其自己的 .h 和 .m 文件中分别创建该类及其所有函数。然后在视图控制器中,使用检索到的数据实例化 twitter 类,并开始在视图控制器中使用它。

您的视图控制器实际上正在检索数据,但您的模型对象是维护数据的对象。通过这种方式,您可以将模型数据与 twitter 对象一起传递给其他视图控制器。

If you think about reusability, the main components that can be reused again are your model objects and view objects. They can be moved to different apps and still used properly. Your view controller is what is really specific to your app and where most of the app logic lies.

So in your example, you could have a twitter object that stores information and tweets from a user perhaps. You would create that class with all its functions separately within its own .h and .m file. Then in your view controller, instantiate the twitter class with data that is retrieved and begin using it from within the view controller.

Your view controller is actually retrieving the data but your model object is the one maintaining the data. In this way, you can pass on the model data with your twitter object to other view controllers.

靖瑶 2024-09-25 02:08:30

对应用程序的控制位于控制器中,因此它是检索或保存持久数据、使用该数据更新视图以及处理各种事件的对象。将其视为模型和视图之间的粘合剂!

例如,如果您要单击一个按钮来打开一个新的模式视图,您将在视图控制器中处理该事件。在响应单击的按钮的方法中,您将创建或访问新的视图控制器并使用presentModalViewController:animated: 呈现它。如果新视图和控制器需要当前控制器有权访问的数据,您可以在新控制器中设置一个属性来引用该对象。

Control over the application resides in the controller, so it is the object that will retrieve or save persisted data, update views with that data, and handle various events. Consider it the glue between the model and the view!

For example, if you were to click on a button to open a new modal view, you'd handle that event in your view controller. In the method that responds to the clicked button, you will create or access the new view controller and present it using presentModalViewController:animated:. If that new view and controller needs data that your current controller has access to, you could set a property in the new controller to refer to the object.

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