浅谈iOS中MVVM设计模式

MVVM

所谓mvvm就是在mvc的基础上将业务逻辑抽离出来放到viewModel中,如下:

1:model层,API请求的原始数据.

2:view层,视图展示,由viewController来控制.

3:viewModel层,负责业务处理和数据转化.

接下来就具体实现MVVM模式:

1:使用AFNetWorking封装数据请求类:BaseNetWork.h


`+(id)POST:(NSString *)urlStr paramenters:(NSDictionary *)parameters completionHandler:(void(^)(id responseObj,NSError *error))completionHandler;`


`BaseNetWork.m

+(id)POST:(NSString *)urlStr paramenters:(NSDictionary *)parameters completionHandler:(void(^)(id responseObj,NSError *error))completionHandler{

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];

return [manager POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

NSLog(@"%@", task.currentRequest.URL.absoluteString);

!completionHandler?:completionHandler(responseObject, nil);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"%@", task.currentRequest.URL.absoluteString);

NSLog(@"%@", error);

!completionHandler ?: completionHandler(nil, error);

}];

}`

2:创建数据模型

#import "LoginModel.h"

3:基于afn基础类的对接口和模型进行封

例如:NetWorkRequest.h

`#import "BaseNetWork.h"

#import "LoginModel.h"

@interface NetWorkRequest : BaseNetWork

+(id)getLoginStatusWith:(NSString*)userName andPassword:(NSString*)password andToken:(NSString*)token andRegistration_id:(NSString*)registration_id completionHandler:(void(^)(LoginModel *model,NSError *error))completionHandler;

@end`

NetWorkRequest.m中实现

#import "NetWorkRequest.h"

#define ADDRESSURL @"http://xgdj.szlcnet.com/index.php?m=Courier&c=User&a=login"

@implementation NetWorkRequest

+(id)getLoginStatusWith:(NSString*)userName andPassword:(NSString*)password andToken:(NSString*)token andRegistration_id:(NSString*)registration_id completionHandler:(void(^)(LoginModel *model,NSError *error))completionHandler{

NSString *netPath = ADDRESSURL;

NSDictionary *parameter = @{@"user_name":userName,@"password":password,@"token":token,@"registration_id":registration_id};

return [self POST:netPath paramenters:parameter completionHandler:^(id responseObj, NSError *error) {

!completionHandler ?: completionHandler([LoginModel parse:responseObj],error);

}];

}

@end

4:创建viewModel的基础类如

BaseViewModel.h

//请求状态

typedef NS_ENUM(NSUInteger, RequestMode) {

RequestModeRefresh,

RequestModeMore,

};

@interface BaseViewModel : NSObject

@property (nonatomic) NSURLSessionDataTask *dataTask;

- (void)getDataWithMode:(RequestMode)requestMode completionHandler:(void(^)(NSError *error))completionHandler;

@end

BaseViewModel.m

#import "BaseViewModel.h"

@implementation BaseViewModel

- (void)getDataWithMode:(RequestMode)requestMode completionHandler:(void (^)(NSError *))completionHandler{

}

@end

5:创建相应的借口VIewModel

例如登入借口model

LoginViewModel.h在这里需要将请求接口中所需传的字段暴露出来

#import "BaseViewModel.h"

#import "NetWorkRequest.h"

#import "LoginModel.h"

#import "BaseViewModel.h"

@interface LoginViewModel : BaseViewModel

@property (nonatomic) LoginModel *data;

@property (nonatomic) NSString *username;

@property (nonatomic) NSString *password;

@property (nonatomic) NSString *token;

@property (nonatomic) NSString *registration_id;

- (NSInteger)loginStatusCode;

- (NSString*)tokenForLoginInformation;

@end

在.m中可以实现

#import "LoginViewModel.h"

@implementation LoginViewModel

-(void)getDataWithMode:(RequestMode)requestMode completionHandler:(void (^)(NSError *))completionHandler{

self.dataTask = [NetWorkRequest getLoginStatusWith:self.username andPassword:self.password andToken:self.token andRegistration_id:self.registration_id completionHandler:^(LoginModel *model, NSError *error) {

if (!error) {

self.data = model;

}

!completionHandler ?: completionHandler(error);

}];

}

- (NSString *)tokenForLoginInformation

{

return self.data.result.token;

}

- (NSInteger)loginStatusCode

{

return self.data.status;

}

@end

6:在控制器中实现借口逻辑拿到数据处理业务

如下

#import "ViewController.h"

#import "LoginViewModel.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

LoginViewModel *viewModel = [[LoginViewModel alloc]init];

//实现请求接口中所需要传的字段

viewModel.username = @"123456";

viewModel.password = @"123456";

viewModel.token = @"";

viewModel.registration_id =@"";

[SVProgressHUD showWithStatus:@"正在获取用户信息……" maskType:SVProgressHUDMaskTypeBlack];

[viewModel getDataWithMode:RequestModeRefresh completionHandler:^(NSError *error) {

[SVProgressHUD dismiss];

//在此处可以拿到数据并处理逻辑业务

NSLog(@"%@",[viewModel tokenForLoginInformation]);

}];

}

@end

你可能感兴趣的:(浅谈iOS中MVVM设计模式)