MVVM设计模式具体实现

前面有一篇我们已经讲到MVVM设计模式的基本原理,网址:http://blog.csdn.net/baihuaxiu123/article/details/50752136
这里就起、其概念就不过多讲了,该篇就来讲讲MVVM这一模式具体实现。主要利用一个页面登录跳转来实现MVVM的设计模式。要实现这一模式借助了IOS响应式编程框架ReactiveCocoa作为辅助。整个目录结构:
MVVM设计模式具体实现_第1张图片

主要代码片段:
LoginViewController.m

#import "LoginViewController.h"
#import "LGViewModel.h"
#import "DisplayViewController.h"
#import 
@interface LoginViewController ()
@property (strong, nonatomic) IBOutlet UIButton *loginButtoon;
@property (nonatomic,strong)LGViewModel *viewModel;
@property (nonatomic,strong)NSArray *dataSource;

@property (nonatomic,strong)NSString *names;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self bindModel];
    [self onClick];
    [RACObserve(self, names) subscribeNext:^(id x) {
        NSLog(@"names = %@",x);
    }];
    self.names = @"ascdv";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//关联ViewModel
- (void)bindModel {
    _viewModel = [[LGViewModel alloc] init];

    RAC(self.viewModel, username) = self.userNameT.rac_textSignal;
    RAC(self.viewModel, password) = self.passWordT.rac_textSignal;
    RAC(self.loginButtoon, enabled) = [_viewModel buttonIsValid];
    
    @weakify(self);
    
    //登录成功要处理的方法
    [self.viewModel.successObject subscribeNext:^(NSArray * x) {
        @strongify(self);
        DisplayViewController *vc = [[DisplayViewController alloc]init];
        vc.userName = x[0];
        vc.password = x[1];
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    
    //fail
    [self.viewModel.failureObject subscribeNext:^(id x) {
        
    }];
    
    //error
    [self.viewModel.errorObject subscribeNext:^(id x) {
        
    }];
    
}
- (void)onClick {
    //按钮点击事件
    [[self.loginButtoon rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
         [_viewModel login];
     }];
}

DisplayViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.usernameLab.text = _userName;
    self.passWordLab.text = _password;
}

LGViewModel.h

#import 
#import 


@interface LGViewModel : NSObject
@property (nonatomic,strong)NSString *username;
@property (nonatomic,strong)NSString *password;
@property (nonatomic,strong)RACSubject *successObject;
@property (nonatomic, strong) RACSubject *failureObject;
@property (nonatomic, strong) RACSubject *errorObject;

- (id) buttonIsValid;
- (void)login;
@end

LGViewModel.m


#import "LGViewModel.h"

@interface LGViewModel ()
@property (nonatomic, strong) RACSignal *userNameSignal;
@property (nonatomic, strong) RACSignal *passwordSignal;
@property (nonatomic, strong) NSArray *requestData;
@end



@implementation LGViewModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    _userNameSignal = RACObserve(self, username);
    _passwordSignal = RACObserve(self, password);
    _successObject = [RACSubject subject];
    _failureObject = [RACSubject subject];
    _errorObject = [RACSubject subject];
}

//合并两个输入框信号,并返回按钮bool类型的值
- (id) buttonIsValid {
    
    RACSignal *isValid = [RACSignal
                          combineLatest:@[_userNameSignal, _passwordSignal]
                          reduce:^id(NSString *userName, NSString *password){
                              return @(userName.length >= 3 && password.length >= 3);
                          }];
    
    return isValid;
}

- (void)login{
    
    //网络请求进行登录
    _requestData = @[_username, _password];
    
    //成功发送成功的信号
    [_successObject sendNext:_requestData];
    
    //业务逻辑失败和网络请求失败发送fail或者error信号并传参
    
}

效果图:
MVVM设计模式具体实现_第2张图片
MVVM设计模式具体实现_第3张图片
具体代码Demo 下载网址:http://pan.baidu.com/s/1bnPQF5l

你可能感兴趣的:(MVVM设计模式具体实现)