在MVC中,你把引用你的模型类? [英] In MVC where do you put references to your model Classes?

查看:400
本文介绍了在MVC中,你把引用你的模型类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经想了一会儿,在问了不同的人,没有任何人提供我称之为至少有点具体的答案:

问题:



在iPhone应用程序中,应用程序应保留对其模型类的引用(使用 MVC 方法)?



在iPhone(和Cocoa)应用程序中,我们有称为App Delegate的应用程序,它基本上启动了我们的应用程序,并在我们的控制器中,也处理UITouch事件。



那么App Delegate是一个控制器吗?一个模型类?没有两个?我想不知道这也让人困惑,知道在哪里放置模型引用。



示例:



具有应用程序委托,该委托包含对您的应用程序的视图控制器的引用。如果我的应用程序将使用模型类A(它是一个webserver守护进程类)和一个类B存储由该web服务器查询的数据。



参考A和B? (App Delegate?View Controller?Both?)



这里有很多选项,但作为一个例子,我真的想知道你们如何使用mvc这个应用程序只使用一个视图。

解决方案

很容易把一切放在AppDelegate中,但如果你开始做,那么你的AppDelegate将会充满引用黑客。如果你正在做一个严格的MVC,那么你应该有3件事情:




  • A模型

  • < View控制器(仅用于查看逻辑)
  • 控制器(用于在视图和模型之间进行协调)



例如,我有一个模型Foo和一个Foo控制器。我会有:




  • Foo.m(Model)

  • FooViewController.m )

  • FooController.m(控制逻辑)



最后, ,我会把我的引用Foo的在foo控制器。我喜欢使用singletons为我的控制器,但这只是我。如果你使用单例,你可以这样做: [[FooController sharedInstance] listOfFoos] 获取你的Foo


I have been wondering for a while, after asking different people and without any of them providing what I would call an "at least a bit concrete answer":

Question:

Where, in an iPhone application should an application keep the references to it's Model Classes (using the MVC approach) ?

In iPhone (and Cocoa) applications we have what we call the "App Delegate", which basically start's up our application and inits our controllers, also handles UITouch events.

So is the App Delegate a controller ? a model class ? none of the two ? I think not knowing that also makes confusing to know where to put the Model References.

Example:

You have the Application Delegate, that delegate contains a reference to your Application's View Controller. If my Application would use Model Class A ( which is a webserver daemon class ), and a Class B which stores data queried by that webserver.

Where would you guys store the references to A and B ? (App Delegate ? View Controller ? Both ? )

There are many options here, but as an example, I would really like to know how would you guys use mvc to put together this application which only uses one View.

解决方案

It is tempting to put everything in the AppDelegate, but if you start doing this, then your AppDelegate will be full of reference hacks. If you are doing a strict MVC, then you should have 3 things:

  • A Model
  • A View Controller (Only for view logic)
  • A Controller (For coordinating between the view and the model)

So for example, I have a model Foo and a Foo controller. I would have:

  • Foo.m (Model)
  • FooViewController.m (Displays a Foo)
  • FooController.m (Controls the logic)

And finally, to answer your question, I would store my references to Foo's in the foo Controller. I like to use singletons for my controllers, but thats just me. If you do use a singleton, you can just do something like this: [[FooController sharedInstance] listOfFoos] to get your Foo's

这篇关于在MVC中,你把引用你的模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆