iOS MVVM架构的介绍(内含Demo)
2016-07-06 10:11
633 查看
iOS MVVM模式(Model-View-ViewModel):
1. Model层是少不了的了,我们得有东西充当DTO(数据传输对象),当然,用字典也是可以的,编程么,要灵活一些。Model层是比较薄的一层,如果学过Java的小伙伴的话,对JavaBean应该不陌生吧。
2. ViewModel层,就是View和Model层的粘合剂,他是一个放置用户输入验证逻辑,视图显示逻辑,发起网络请求和其他各种各样的代码的极好的地方。说白了,就是把原来ViewController层的业务逻辑和页面逻辑等剥离出来放到ViewModel层。
3. View层,就是ViewController层,他的任务就是从ViewModel层获取数据,然后显示。
MVVM实现的基本条件(限本博客):
- 1. JSONModel
- 2. ReactiveCocoa(响应式编程)
- 3. AFNetworking
- 4. AFNetworking-RACExtensions–>AFNetworking-RACExtensions
- 5. JSONModel-RACExtension–>JSONModel-RACExtensions
- 6. 此外需要在自己的mac上搭建虚拟服务器模拟网络请求
- 7. CocoaPods的安装使用
废话不多说,看部分代码(代码较多)!
demo: –> XCMVVM
1. Model层是少不了的了,我们得有东西充当DTO(数据传输对象),当然,用字典也是可以的,编程么,要灵活一些。Model层是比较薄的一层,如果学过Java的小伙伴的话,对JavaBean应该不陌生吧。
2. ViewModel层,就是View和Model层的粘合剂,他是一个放置用户输入验证逻辑,视图显示逻辑,发起网络请求和其他各种各样的代码的极好的地方。说白了,就是把原来ViewController层的业务逻辑和页面逻辑等剥离出来放到ViewModel层。
3. View层,就是ViewController层,他的任务就是从ViewModel层获取数据,然后显示。
MVVM实现的基本条件(限本博客):
- 1. JSONModel
- 2. ReactiveCocoa(响应式编程)
- 3. AFNetworking
- 4. AFNetworking-RACExtensions–>AFNetworking-RACExtensions
- 5. JSONModel-RACExtension–>JSONModel-RACExtensions
- 6. 此外需要在自己的mac上搭建虚拟服务器模拟网络请求
- 7. CocoaPods的安装使用
废话不多说,看部分代码(代码较多)!
Model文件夹
#TestModel.h文件 #import "XCBaseModel.h" #import "userinfoModel.h" @interface TestModel : XCBaseModel @property NSString* city; @property userinfoModel *user_info; @end #import <JSONModel/JSONModel.h> @interface userinfoModel : JSONModel @property NSString* user_name; @property NSNumber* user_code; @end
NetWork文件夹
XCAPIManager+test.m #import "XCAPIManager+test.h" #import "XCAPIManager+Analysis.h" #import "TestModel.h" @implementation XCAPIManager (test) - (RACSignal *)test { //此处使用的mac自带的模拟服务器 return [[[[[self rac_GET:@"http://localhost/~liuxiaochun/a.json"(http://yangjunwei.com/a/1568.html) parameters:@{}] map:^id(id value) { return [self analysisRequest:value]; }] flattenMap:^RACStream *(id value) { if([value isKindOfClass:[NSError class]]){ return [RACSignal return:value]; } return [self rac_MappingForClass:[TestModel class] array:value[@"topic_infos"]]; }] logError] replayLazily]; } @end
View/ViewController文件夹
ViewController.m #import "ViewController.h" #import "TestViewModel.h" #import "XCTableviewCellTest.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-property-synthesis" @property (strong, nonatomic) IBOutlet TestViewModel *viewModel; #pragma clang diagnostic pop @property (nonatomic,strong)NSArray* models; @property (weak, nonatomic) IBOutlet UITableView *tableview; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; @weakify(self) [self.viewModel.testCommand execute:nil]; [RACObserve(self.viewModel, tests) subscribeNext:^(NSArray* x) { @strongify(self) self.models = x; [self.tableview reloadData]; }]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - delegate / datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.models.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* cellId = @"cell"; XCTableviewCellTest* cell = [tableView dequeueReusableCellWithIdentifier:cellId]; [cell bindModel:self.models[indexPath.row]]; return cell; } ----------------- XCTableviewCellTest.m #import "XCTableviewCellTest.h" @implementation XCTableviewCellTest - (void)bindModel:(TestModel *)model { self.data1.text = model.city; self.data2.text = model.user_info.user_name; self.data3.text = [NSString stringWithFormat:@"%@",model.user_info.user_code]; } @end
ViewModel文件夹
#import "TestViewModel.h" #import "XCAPIManager+test.h" @interface TestViewModel () @property (nonatomic,strong,readwrite) RACCommand* testCommand; @property (nonatomic,strong,readwrite) NSArray* tests; @end @implementation TestViewModel - (instancetype)init { if (self = [super init]) { } return self; } - (RACCommand *)testCommand { if (!_testCommand) { @weakify(self) _testCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) { @strongify(self) return [self.apiManager test]; }]; [[_testCommand.executionSignals concat] subscribeNext:^(NSArray* x) { @strongify(self) self.tests = x; NSLog(@"%@",x); }]; // XPViewModelShortHand(_testCommand) } return _testCommand; } @end
demo: –> XCMVVM
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 架构纵横谈之二 ---- 架构的模式与要点
- BS项目中的CSS架构_仅加载自己需要的CSS
- WPF MVVM示例讲解
- 关于三种主流WEB架构的思考
- Android操作系统的架构设计分析
- 浅谈JavaScript前端开发的MVC结构与MVVM结构
- w3c技术架构介绍
- linux学习笔记 linux目录架构
- ASP.NET小结之MVC, MVP, MVVM比较以及区别(一)
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结