简体   繁体   中英

What is the difference between a Model (MVC) in web development compared to iOS development

I come from a Web Development background. I am having trouble understanding how a model is to be used in iOS development.

A model in Web Development is a separate file that Controller files access with functions such as getMysqlQueryOf(stuff);

Does iOS development use the same thing? 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?

Can you show a simple example of a Model class and how it would be accessed from a View Controller?

In my iOS experiance, UIViewControllers tend to act as islands unto themselves. 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.

IMO, iOS data flow leaves something to be desired.

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].

MyData.h

@interface MyData : NSObject {
    NSString *dataName; // example data in my object
}
@property (nonatomic, retain) NSString *dataName;
+ (MyData *)getMyData;

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM