来自 sublime 文本编辑器中的 ruby on rails,我的文件夹布局有模型、 View 和 Controller 文件夹,他的文件夹布局很容易识别什么是什么。
在 objective-c 中,我刚刚为我正在学习的新教程创建了一个新项目。我有文件:
BullsEyeViewController.h BullsEyeViewController.m BullsEyeViewController.xib
看来xib文件就是 View 。我知道 .h 文件用于声明,而 .m 文件用于我对这些声明的实现。这 2 个文件会被归类为 Controller 吗?
如果是这样,我将如何识别模型?我已经完成了 big-nerd-ranch 的枯燥但必要的 objective-c 编程书,但当涉及到 objective c 中的 MVC 时,我仍然发现自己有点困惑。
我现在正在上另一门类(class),我想我应该一劳永逸地解决这个问题。正如我所说,在 ruby on rails 中,我可以轻松识别模型 View 和 Controller ,但在 objetive-c 中,当我尝试时,我开始感到困惑。
xib是一个 View ?但是它的名字中有 viewcontroller,这让我有点不解。将不胜感激一些帮助清理这一切。比方说,我想创建一个文件来处理我的所有存储并与数据库通信。这在文件名结构方面看起来如何?
另外,我现在将 appdelegate 文件放在支持文件夹中,以减少混淆。 我想做的是创建一个模型、 View 和 Controller 文件夹,并将相关文件放入其中。
最佳答案
我只是维护这个结构。
|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.
例子。
|view
|--login.xib
|Controller
|--loginViewController.h
|--loginViewController.m
|model
|--loginModel.h
|--loginModel.m
在 LoginModel 中,我通常处理 API 调用并将所有 json 转换为 NSMutableDictionary 对象并将数据发送到 Controller 以进行进一步处理。
在我的 loginController 中,它将对接收到的数据执行操作。
示例:我正在使用 AFNetworking 进行 API 调用。
AFHTTPAPIClient.h
#import "AFHTTPClient.h"
#import "AFJSONRequestOperation.h"
@interface AFHTTPAPIClient : AFHTTPClient
+ (AFHTTPAPIClient *)sharedClient;
@end
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
loginModel.h
#import "AFHTTPAPIClient.h"
+ (void)loginWith:(NSDictionary *)parameters withBlock:(void (^)(NSMutableDictionary *, NSError *))block;
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);
}
}];
}
然后最终在 Controller 中使用它们。
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.
}
}];
关于ios - 如何在 objective-c 中识别模型、 View 和 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18805981/