简单的MVC设置/设计? [英] Simple MVC setup / design?

查看:132
本文介绍了简单的MVC设置/设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

// MODEL
@interface Reactor:NSObject {
NSNumber * temperature;
NSString * lastInspection;
NSNumber * activeRods;
}

  // CONTROLLER 
@interface viewController:UIViewController {
UITextField * tempTextField;
UITextField * inspectTextField;
UITextField * activeTextField;
反应堆*反应堆;
}
@property ...
...
- (IBAction)ButtonPressed;
@end



我是在声明反应器(dataModel)在控制器?我看到它的方式,控制器位于[VIEW]和[MODEL]之间,所以它需要链接到两者。



[VIEW]通过IBActions链接到[CONTROLLER]中实现的方法,后者又将消息发送到[MODEL]中的方法。 [MODEL]需要任何处理/数据处理,将任何所需结果发送回[CONTROLLER],[CONTROLLER]可用于更新[VIEW]。



这是合理的吗?



gary

解决方案

你可能冒险这是一个非常小,简单的模型,但你真的不应该开发它作为一个习惯,因为这种方法将分解,随着你的项目越来越大。



如果您的应用程式有两个或以上的资料检视进出模型,会发生什么情况?当您拥有包含多个数据视图的应用并从网址获取数据时会发生什么情况?如果要在html或甚至命令行界面中使用数据模型,会发生什么。当你想在另一个应用程序中使用数据模型时会发生什么?



如果你要编写严重的代码,会有成千上万的用户,多个版本和可能的分裂,你需要早日发展良好的习惯。



数据模型的最佳方法是创建一个单例来保存它。我建议创建一个通用单例类,然后使任何特定项目的模型为该类的子类。这样,你有一个整洁的包含模型,可以从你的应用程序的任何地方访问。



第二好的方法是将数据模型对象停放在应用程式委托中。但是,随着项目规模的扩大,这可能变得不方便。



这当然不会帮助我看到的每一条教学/教程信息都忽略了数据模型,有利于教你如何做眼睛糖果接口。数据模型只是简单的数组或一些这样的粘在控制器的侧面。



相信我,你不希望成长和增长你的应用程序的噩梦只发现它的数据模型逻辑和数据分散在十几个视图控制器。磨牙,从正确的行动,你永远不会出汗。



Edit01:


Singleton,我必须做一些
研究,你能解释一下如何
你会从
[控制器]访问?只是好奇,如果
[MODEL]再次在AppDelegate
如何从
[CONTROLLER]访问?


您可以通过(1)使数据模型对象成为应用程序委托的属性,(2)使用应用程序委托的全局引用形式,将数据模型驻留在应用程序委托中:

  MyDataModelClass * theModel = [[UIApplication sharedApplication] delegate] dataModelProperty]; 

创建 singleton 是一个稍微涉及更多。你像使用NSFileManger一样使用它。您可以以同样的方式引用它。

  NSFileManger * fm = [NSFileManager sharedInstance]; 

这可确保应用程序中只有一个文件管理器处于活动状态。您将从控制器以相同的方式调用数据模型:



MyDataModelClass * theModel = [MyDataModelClass sharedInstance];



如果你仔细设计数据模型对象,以便它可以完全控制写入它的信息,你可以安全地使用它在任何地方,而不必担心你的数据将被一个粗心的代码行。


I just want to make sure that I am heading in the right direction with how a simple MVC application is constructed.

// MODEL
@interface Reactor: NSObject {
    NSNumber *temperature;
    NSString *lastInspection;
    NSNumber *activeRods;   
}

.

// CONTROLLER
@interface viewController: UIViewController {
    UITextField *tempTextField;
    UITextField *inspectTextField;
    UITextField *activeTextField;
    Reactor *reactor;
}
@property ...
...
-(IBAction)ButtonPressed;
@end

.

Am I right in declaring the reactor (dataModel) in the controller? The way I see it the controller sits between the [VIEW] and the [MODEL] so its going to need links to both.

The [VIEW] is linked via "IBActions" to methods implemented within the [CONTROLLER] which in turn sends messages to methods in the [MODEL]. The [MODEL] does any processing/data manipulation needed, sending any required results back to the [CONTROLLER] which in turn may be used to update the [VIEW].

Does this sound sensible, in simple terms?

gary

解决方案

You can risk this for a very small, simple model but you really shouldn't develop it as a habit because this method will breakdown as your projects get larger.

What happens when you have an app two or more views both taking data in and out of the model? What happens when you have an app with multiple views and gets data from a URL? What happens if you want to use the data model in an html or even a command line interface. What happens when you want to use the data model in another app altogether?

If your going to be writing serious code that will have thousands of users, multiple versions and possibly spinoffs, you need to develop good habits early.

The best method for a data model is to create a singleton to hold it. I would suggest creating a generic singleton class and then making your model for any particular project a subclass of that class. That way you have a neat contained model that can be accessed from anywhere in your app.

The second best method is to park the data model object in the app delegate. However, that to can become unwieldily as the project scales.

It certainly doesn't help that every piece of instructional/tutorial information I have seen pretty much ignores the data model in favor of teaching you how to do the eye candy for the interface. The data models are just simple arrays or some such tacked onto the side of a controller.

Trust me, you don't want the nightmare of growing and growing your app only to discover that its data model logic and data are scattered over a a dozen view controllers. Grit your teeth and do it correctly from the get-go and you'll never have to sweat it.

Edit01:

Singleton, I will have to do some research, can you explain a little how you would access that from the [CONTROLLER]? Just curious if the [MODEL] goes in the AppDelegate again how would you access it from the [CONTROLLER]?

You park a data model in the app delegate by (1) making the data model object a property of the app delegate and (2) using the global reference form for the app delegate:

MyDataModelClass *theModel=[[UIApplication sharedApplication] delegate] dataModelProperty];

Creating a singleton is a little more involved. You use it like you use the NSFileManger. You would reference it the same way.

NSFileManger *fm=[NSFileManager sharedInstance];

This ensures that only one file manager is active in an app. You would call the data model the same way from a controller:

MyDataModelClass *theModel=[MyDataModelClass sharedInstance];

If you carefully design the data model object such that it has complete control over what information is written to it, you can safely use it anywhere without having to worry that your data will be trashed by a one careless line of code.

这篇关于简单的MVC设置/设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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