針對MVC和MVVM這兩種構(gòu)建模式谊迄,網(wǎng)上隨便一百度就是一大堆針對這兩種構(gòu)建模式的解說诸迟,總結(jié)起來無非以下兩點(diǎn):
- MVC(Model View Controller)是一種成熟的奈籽,蘋果推薦的一個(gè)用來組織代碼的權(quán)威范式唬渗,日常用的最多的一種構(gòu)建模式慎框,缺點(diǎn)就是viewController里面代碼量厚重,耦合性強(qiáng)浸颓;
- MVVM(Model View View-Mode)是MVC衍生出來一種維護(hù)性較強(qiáng)笨枯、耦合性低的新的架構(gòu)坎藐,它正式規(guī)范了視圖和控制器緊耦合的性質(zhì),并引入新的組件能颁,分離了視圖(View)和模型(Model)杂瘸, 用戶輸入驗(yàn)證邏輯,視圖顯示邏輯伙菊,發(fā)起網(wǎng)絡(luò)請求等代碼的都放在viewModel中處理败玉,從而大大的減少了viewController的代碼量。
如果想深入了解他們的區(qū)別占业,就請移步自行Google吧绒怨,這里就不贅述了。對于這些東西谦疾,只有自己寫過了南蹂,才能慢慢的從中體會到他們的差別,至于哪個(gè)好哪個(gè)適合自己念恍,就只能自己從中取舍了六剥。
下面講述的是一個(gè)結(jié)合了三方ReactiveObjC
晚顷,采用MVVM的構(gòu)建模式結(jié)合RAC寫了一個(gè)登錄的小功能,從demo中疗疟,能夠很清楚的看到该默,viewController中的代碼量大大的減少了,至少達(dá)到了V瘦身的效果??策彤。
首先要pod ReactiveObjC到項(xiàng)目當(dāng)中
先來看view栓袖,里面處理簡單的頁面布局和一些判斷
DCLoginView.h
@property (strong, nonatomic)UITextField *usernameTextField;//用戶名
@property (strong, nonatomic)UITextField *passwordTxtField;、店诗、密碼
@property (strong, nonatomic)UIButton *signInButton;//登陸按鈕
@property (strong, nonatomic)UIButton *signUpButton;//注冊按鈕
@property(nonatomic,assign)BOOL isValidate;//判斷用戶名和密碼是否為空
DCLoginView.m
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUpLoginView];
}
return self;
}
-(void)setUpLoginView
{
//用戶名
_usernameTextField = [[UITextField alloc]init];
_usernameTextField.placeholder = @"用戶名";
_usernameTextField.textColor = [UIColor blackColor];
_usernameTextField.font = [UIFont systemFontOfSize:15];
_usernameTextField.backgroundColor = [UIColor lightGrayColor];
_usernameTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
_usernameTextField.layer.borderWidth = 0.6;
_usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self addSubview:_usernameTextField];
//密碼
_passwordTxtField = [[UITextField alloc]init];
_passwordTxtField.placeholder = @"密碼";
_passwordTxtField.textColor = [UIColor blackColor];
_passwordTxtField.font = [UIFont systemFontOfSize:15];
_passwordTxtField.secureTextEntry = YES;
_passwordTxtField.backgroundColor = [UIColor lightGrayColor];
_passwordTxtField.layer.borderColor = [UIColor lightGrayColor].CGColor;
_passwordTxtField.layer.borderWidth = 0.6;
_passwordTxtField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self addSubview:_passwordTxtField];
//登陸
_signInButton = [UIButton buttonWithType:UIButtonTypeCustom];
_signInButton.layer.cornerRadius = 4;
_signInButton.layer.masksToBounds = YES;
[_signInButton setTitle:@"登錄" forState:UIControlStateNormal];
[_signInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_signInButton.backgroundColor = [UIColor redColor];
[self addSubview:_signInButton];
//登陸
_signUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
_signUpButton.layer.cornerRadius = 4;
_signUpButton.layer.masksToBounds = YES;
[_signUpButton setTitle:@"注冊" forState:UIControlStateNormal];
[_signUpButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_signUpButton.backgroundColor = [UIColor redColor];
[self addSubview:_signUpButton];
}
-(void)layoutSubviews
{
[super layoutSubviews];
_usernameTextField.frame = CGRectMake((self.width - 150)/2, 190, 150, 40);
_passwordTxtField.frame = CGRectMake((self.width - 150)/2, CGRectGetMaxY(_usernameTextField.frame)+10, 150, 40);
_signInButton.frame = CGRectMake((self.width - 180)/2, CGRectGetMaxY(_passwordTxtField.frame)+30, 80, 40);
_signUpButton.frame = CGRectMake(CGRectGetMaxX(_signInButton.frame)+20, _signInButton.y, _signInButton.width, _signInButton.height);
}
-(BOOL)isValidate
{
if ([_usernameTextField.text isEqualToString:@""]) {
return NO;
}
if ([_passwordTxtField.text isEqualToString:@""]) {
return NO;
}
return YES;
}
再來看viewModel裹刮,拿到view中的用戶輸入的用戶名和密碼,利用RAC發(fā)射信號庞瘸,里面處理一些網(wǎng)絡(luò)請求
DCLoginViewModel.h
#import <Foundation/Foundation.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import "DCLoginView.h"
@interface DCLoginViewModel : NSObject
@property(nonatomic,strong)RACCommand * loginCommond;
@property (nonatomic,copy)NSString *username;
@property (nonatomic,copy)NSString *password;
@property (nonatomic,strong) DCLoginView *loginView;
@end
DCLoginViewModel.m
-(void)setLoginView:(DCLoginView *)loginView
{
_loginView = loginView;
RAC(self, username) = loginView.usernameTextField.rac_textSignal;
RAC(self, password) = loginView.passwordTxtField.rac_textSignal;
}
- (instancetype) init
{
if (self = [super init]) {
[self setup];
}
return self;
}
- (void) setup {
__weak typeof(self) weakself = self;
self.loginCommond = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
if (!weakself.loginView.isValidate) {
[[weakself getCurrentVC] showMessage:@"賬戶名或密碼不能為空捧弃!" afterDelay:1.0];
[subscriber sendCompleted];
return nil;
}
//處理網(wǎng)絡(luò)數(shù)據(jù)請求,請求成功之后跳轉(zhuǎn)
if ([weakself.loginView.usernameTextField.text isEqualToString:@"123"]&&[weakself.loginView.passwordTxtField.text isEqualToString:@"123"]) {
[[weakself getCurrentVC] showMessage:@"驗(yàn)證成功之后跳轉(zhuǎn)到首頁" afterDelay:1.0 completion:^{
[[weakself getCurrentVC] presentViewController:[[HomeViewController alloc]init] animated:YES completion:nil];
}];
}else
{
[[weakself getCurrentVC] showMessage:@"賬戶名或密碼錯(cuò)誤擦囊!賬戶名:123 密碼:123" afterDelay:2.0];
}
[subscriber sendCompleted];
return nil;
}];
}];
}
@end
最后就是viewController了违霞,里面只處理接收viewModel發(fā)過來的信號,關(guān)聯(lián)viewModel和View
ViewController.m
#import "ViewController.h"
#import "DCLoginView.h"
#import "DCLoginViewModel.h"
@interface ViewController ()
@property (nonatomic,strong) DCLoginView *loginView;
@property (nonatomic,strong) DCLoginViewModel *loginViewModel;
@end
@implementation ViewController
-(void)loadView
{
DCLoginView *loginView = [[DCLoginView alloc]init];
self.loginView = loginView;
self.view = loginView;
}
-(DCLoginViewModel *)loginViewModel
{
if (!_loginViewModel) {
_loginViewModel = [DCLoginViewModel new];
}
return _loginViewModel;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.loginViewModel.loginView = self.loginView;
[self addLoginViewAction];
}
-(void)addLoginViewAction{
#pragma mark - 登陸
__weak typeof(self) weakself = self;
[[self.loginView.signInButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
[self.loginViewModel.loginCommond execute:nil];
NSLog(@"signInButtonDidClick");
}];
#pragma mark - 注冊
[[self.loginView.signUpButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
[weakself showMessage:@"點(diǎn)擊了注冊按鈕" afterDelay:1.0];
}];
}
@end
viewController里面50行代碼不到就能搞定了一個(gè)登錄頁瞬场。
RACCommand
和RACSignal
這兩個(gè)類里面還有很多個(gè)方法买鸽,至于具體怎么用可以直接百度Google。
demo傳送門泌类,如果覺得喜歡的話癞谒,請給個(gè)星支持一下哦????