[英]What is the difference between a Model (MVC) in web development compared to iOS development
I come from a Web Development background. 我来自Web开发背景。 I am having trouble understanding how a model is to be used in iOS development.
我无法理解如何在iOS开发中使用模型。
A model in Web Development is a separate file that Controller files access with functions such as getMysqlQueryOf(stuff);
Web开发中的模型是一个单独的文件,Controller文件通过
getMysqlQueryOf(stuff);
等函数进行访问getMysqlQueryOf(stuff);
Does iOS development use the same thing? iOS开发使用相同的东西吗? Should I be creating a MyModel.h and MyModel.m file and including/importing this in all of my View Controllers and accessing the methods inside the model class like that?
我应该创建一个MyModel.h和MyModel.m文件,并在我的所有View控制器中包含/导入它并访问模型类中的方法吗?
Can you show a simple example of a Model class and how it would be accessed from a View Controller? 您能否展示一个Model类的简单示例以及如何从View Controller访问它?
In my iOS experiance, UIViewControllers tend to act as islands unto themselves. 在我的iOS体验中,UIViewControllers倾向于充当自己的岛屿。 The wrong approach is to put everything you would put into a model into the delegate class instead.
错误的方法是将您放入模型的所有内容放入委托类中。 I think your idea of a "Model" is spot on.
我认为你对“模型”的想法很明显。 I would create a someModel.m and a header file and give every class that needs the model a pointer to it and initialize the core model in the delegate.
我将创建一个someModel.m和一个头文件,并为每个需要该模型的类提供指向它的指针,并在委托中初始化核心模型。
IMO, iOS data flow leaves something to be desired. IMO,iOS数据流还有待改进。
If I have a data model that I want to share across various objects, I create it so that I can get access to it statically. 如果我有一个我希望在各种对象之间共享的数据模型,我创建它以便我可以静态访问它。
For example, the code below creates one object dataHandle that can be easily accessed by any other object in the project by including the header file and calling [MyData getMyData]. 例如,下面的代码创建了一个对象dataHandle,通过包含头文件和调用[MyData getMyData],项目中的任何其他对象都可以轻松访问它。
MyData.h MyData.h
@interface MyData : NSObject {
NSString *dataName; // example data in my object
}
@property (nonatomic, retain) NSString *dataName;
+ (MyData *)getMyData;
MyData.m MyData.m
@implementation MyData
@synthesize dataName;
static MyData *dataHandle;
+ (void)initData
{
dataHandle = [MyData new];
[dataHandle setDataName:@"DefaultName"];
}
+ (MyData *)getMyData
{
if (!dataHandle){
[MyData initData];
}
return dataHandle;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.