MVVM
Model-view-viewmodel
MVVM is actually an evolutionary version of MVC, which decouples the business logic from the VC to ViewModel to achieve the lean of the VC.
Make a simple login decision:
Create Loginviewmodel (logical processing), Loginmodel (data only), Loginviewcontroller. There is no need to loginview in order to better focus on decoupling with ViewModel.
Adding methods to Loginmodel
//.h- (instancetype)initWithUserName:(NSString *)username password:(NSString *)password;@property (nonatomic,copy,readonly)NSString * username;@property (nonatomic,copy,readonly)NSString * password;
//.m- (instancetype)initWithUserName:(NSString *)username password:(NSString *)password { if (self = [super init]) { _username = username; _password = password; } return self;}
Adding methods to Loginviewmodel
//.h
import "PersonModel.h"- (instancetype)initWithPerson:(PersonModel *)person;@property (nonatomic,assign,readonly)BOOL canLogin;
//.m
- (instancetype)initWithPerson:(PersonModel *)person {
if (self = [super init]) { //在这做你绑定model后的处理 _canLogin = [self valiCanLoginWithUserName:person.username password:person.password]; } return self;}- (BOOL)valiCanLoginWithUserName:(NSString *)username password:(NSString *)password { if (username.length & password.length) { return YES; } else { return NO; }}
Then VC (or view) can directly get the result of judgment
PersonModel * person = [[PersonModel alloc]initWithUserName:@"10" password:@"10"];PersonViewModel * viewModel = [[PersonViewModel alloc]initWithPerson:person];NSLog(@"%d",viewModel.canLogin);
Simple features are nothing, and when you deal with complex logic judgments, MVVM has a huge advantage.
IOS schema Mode MVVM