MVVM架构大家应该或多或少的了解过,在iOS开发中大家用的比较多的MVVM架构,肯定是通过ReactiveCocoa第三方框架来实现,那么在iOS环境中还可以通过什么方式来实现MVVM呢?今天给大家提供一种方式:通过KVO的模式来实现,废话补多少,直接上代码。
首先说一下实现思路,MVVM的本质就是viewmodel和view、model实现双向绑定,一一对应,一一绑定。
#import "MainVC.h"
#import "Person.h"
#import "personView.h"
#import "PersonViewModel.h"
@interface MainVC ()
@property(nonatomic,strong)Person *person;
@property(nonatomic,strong)personView *view_person;
@property(nonatomic,strong)PersonViewModel *viewmodel_person;
@end
@implementation MainVC
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [Person new];
self.person.name = @"我小时候外号叫糯米粽子";
self.view_person = [personView new];
self.view_person.frame = self.view.bounds;
[self.view addSubview:self.view_person];
self.viewmodel_person = [PersonViewModel new];
//把model和viewmodel绑定到一起
self.viewmodel_person.person = self.person;
//把viewmodel和view绑定到一起(用kvo来实现)
self.view_person.vm = self.viewmodel_person;
}
View的实现:
#import
#import "PersonViewModel.h"
@interfacepersonView :UIView
@property(nonatomic,strong)PersonViewModel *vm;
@end
#import "personView.h"
@interface personView()
@property(nonatomic,strong)UILabel *label_content;
@property(nonatomic, strong) UIButton *printButton;
@end
@implementation personView
- (instancetype)init{
if(self== [superinit]){
self.label_content= [UILabelnew];
self.label_content.frame=CGRectMake(100,100,200,30);
self.label_content.textColor= [UIColorredColor];
self.label_content.font= [UIFontsystemFontOfSize:16.0];
[selfaddSubview:self.label_content];
if(!_printButton) {
_printButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_printButtonsetFrame:CGRectMake(0,300,80,30)];
[_printButton setTitle:@"Print" forState:UIControlStateNormal];
[_printButtonaddTarget:self
action:@selector(onPrintClik)
forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:_printButton];
}
}
return self;
}
- (void)onPrintClik{
[self.vm onButtonClick];
}
- (void)setVm:(PersonViewModel*)vm{
if(!_vm) {
_vm= vm;
[self.vm addObserver:self forKeyPath:@"content" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{
NSString *newContent = change[NSKeyValueChangeNewKey];
self.label_content.text = newContent;
}
@end
Model实现:
@interface Person : NSObject
@property(nonatomic,copy)NSString *name;
@end
#import "Person.h"
@implementation Person
@end
ViewModel的实现:
#import
#import "Person.h"
@interfacePersonViewModel :NSObject
@property(nonatomic,copy)NSString *content;
@property(nonatomic,strong)Person *person;
- (void)onButtonClick;
@end
#import "PersonViewModel.h"
@implementationPersonViewModel
- (void)setPerson:(Person*)person{
_person= person;
self.content = self.person.name;
}
- (void)onButtonClick{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSStringstringWithFormat:@"%0.f", a];//转为字符型
self.content= timeString;
}
@end
至此,完成了通过KVO实现MVVM。
附上源码:https://github.com/dongzhijiaren/KVO-MVVM.git
转载请注明出处