如何在Objective-C中识别模型,视图和控制器?

Lon*_*Guy 2 model-view-controller cocoa cocoa-touch objective-c ios

来自Sublime Text编辑器中的ruby on rails,我的文件夹布局包含模型,视图和控制器文件夹,他的文件夹可以轻松识别出什么。

在Objective-C中,我刚刚为要关注的新教程创建了一个新项目。我有文件:

BullsEyeViewController.h BullsEyeViewController.m BullsEyeViewController.xib

看来xib文件是视图。我知道.h文件用于声明,.m文件用于我的实现。这两个文件会被归类为控制器吗?

如果是这样,我将如何确定模型?我已经完成了书呆子但又很有必要的Objective-C编程书,但是当谈到目标C中的MVC时,我仍然感到有些困惑。

我现在正在做另一门课程,我想我将一劳永逸地解决此问题。正如我所说,在Ruby on Rails中,我可以轻松识别模型视图和控制器,但是在objetive-c中,当我尝试混淆时。

xib是一个视图吗?但是它的名字有viewcontroller,这使我略微有些扭曲。希望能有一些帮助清除所有这些。可以说我想创建一个处理所有存储并与db通信的文件。从文件名结构的角度来看,这看起来如何?

另外,我现在将appdelegate文件放在了支持文件夹中,以减少混乱。我想做的是创建一个模型,视图和控制器文件夹,并将相关文件放入其中。

Ayu*_*ush 5

我只是保持这种结构。

|view
|--all the .xib's and Storyboard
|Controller
|--All the View Controller both .h and .m file
|model
|--.h and .m file for each controller which would be call models.
Run Code Online (Sandbox Code Playgroud)

例。

|view
|--login.xib
|Controller
|--loginViewController.h
|--loginViewController.m
|model
|--loginModel.h
|--loginModel.m
Run Code Online (Sandbox Code Playgroud)

在LoginModel中,我通常处理API调用,并将所有json转换为NSMutableDictionary对象,然后将数据发送到控制器进行进一步处理。

在My loginController中,它将对接收到的数据执行操作。

示例:我正在使用AFNetworking进行API调用。

AFHTTPAPIClient.h

#import "AFHTTPClient.h"
#import "AFJSONRequestOperation.h"

@interface AFHTTPAPIClient : AFHTTPClient

+ (AFHTTPAPIClient *)sharedClient;

@end
Run Code Online (Sandbox Code Playgroud)

AFHTTPAPIClient.m

#import "AFHTTPAPIClient.h"

@implementation AFHTTPAPIClient

// Singleton Instance
+ (AFHTTPAPIClient *)sharedClient {
    static AFHTTPAPIClient *sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClient = [[AFHTTPAPIClient alloc] initWithBaseURL:[NSURL URLWithString:@"www.yourdomain.com"]];
    });
    return sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {

    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

loginModel.h

#import "AFHTTPAPIClient.h"

+ (void)loginWith:(NSDictionary *)parameters withBlock:(void (^)(NSMutableDictionary *, NSError *))block;
Run Code Online (Sandbox Code Playgroud)

LoginModel.m

+ (void)loginWith:(NSDictionary *)parameters withBlock:(void (^)(NSMutableDictionary *, NSError *))block {
[[AFHTTPAPIClient sharedClient] postPath:@"/api/login" parameters:parameters
                                 success:^(AFHTTPRequestOperation *operation, id responseObject){
                                     NSLog(@"%@",responseObject);
                                     if (block) {
                                         block(responseObject, nil);
                                     }

                                 }
                                 failure:^(AFHTTPRequestOperation *operation,NSError *error){
                                     if (block) {
                                         block([NSArray array], error);
                                     }
                                 }];
}
Run Code Online (Sandbox Code Playgroud)

然后最终在Controllers中使用它们。

loginViewController.m

NSDictionary * loginParametes = [[NSDictionary alloc] initWithObjectsAndKeys:_txtUsername.text,@"username", _txtPassword.text,@"password",nil];
[LoginModel loginWith:loginParametes withBlock:^(NSMutableDictionary *loginInfo, NSError *error) {
    if (!error) {                    
        if([[loginInfo objectForKey:@"status"] boolValue]==YES){
                // Login Success
        }
        else {
                // Login Failure
        }
    }
    else {
                // API Responce failure if content type is not a valid json or valid status code.
    }
}];
Run Code Online (Sandbox Code Playgroud)