MVVM设计模式具体实现
来源:互联网 发布:新浪网络写手在哪投稿 编辑:程序博客网 时间:2024/06/10 00:12
前面有一篇我们已经讲到MVVM设计模式的基本原理,网址:http://blog.csdn.net/baihuaxiu123/article/details/50752136
这里就起、其概念就不过多讲了,该篇就来讲讲MVVM这一模式具体实现。主要利用一个页面登录跳转来实现MVVM的设计模式。要实现这一模式借助了IOS响应式编程框架ReactiveCocoa作为辅助。整个目录结构:
主要代码片段:
LoginViewController.m
#import "LoginViewController.h"#import "LGViewModel.h"#import "DisplayViewController.h"#import <ReactiveCocoa/ReactiveCocoa.h>@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 <Foundation/Foundation.h>#import <ReactiveCocoa/ReactiveCocoa.h>@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信号并传参}
效果图:
具体代码Demo 下载网址:http://pan.baidu.com/s/1bnPQF5l
0 0
- MVVM设计模式具体实现
- MVVM设计模式
- MVVM设计模式
- MVVM设计模式
- MVVM设计模式讲解
- iOS MVVM设计模式
- iOS-MVVM设计模式
- iOS设计模式--MVVM
- MVVM设计模式
- 关于MVVM设计模式
- iOS-MVVM设计模式
- 关于MVVM设计模式
- iOS-MVVM设计模式
- MVVM设计模式
- android设计模式MVVM
- MVVM设计模式
- 大话MVVM设计模式
- 设计模式之 MVVM
- hdoj Ipad,IPhone 3802 (矩阵连乘) 好题
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 004_Http之response响应头
- jqGrid表格多次点击行交替选中的实现
- HDU 3518 Boring counting(后缀数组入门题)
- MVVM设计模式具体实现
- 【深入Java虚拟机】之一:Java内存区域与内存溢出
- ComponentOne 2016 年产品规划
- solr windows linux 下环境搭建
- 基于 Apache 在本地配置多个虚拟主机
- 【rabbitmq】解决SimpleAmqpClient创建连接时阻塞的问题
- Ulua_toLua_基本案例(二)_ScriptsFromFile
- widows 下git记住账号密码
- Ulua_toLua_基本案例(三)_CallLuaFunction