iOS MVVM結合RAC實現一個簡單的登入功能
阿新 • • 發佈:2019-01-27
針對MVC和MVVM這兩種構建模式,網上隨便一百度就是一大堆針對這兩種構建模式的解說,總結起來無非以下兩點:
- MVC(Model View Controller)是一種成熟的,蘋果推薦的一個用來組織程式碼的權威正規化,日常用的最多的一種構建模式,缺點就是viewController裡面程式碼量厚重,耦合性強;
- MVVM(Model View View-Mode)是MVC衍生出來一種維護性較強、耦合性低的新的架構,它正式規範了檢視和控制器緊耦合的性質,並引入新的元件,分離了檢視(View)和模型(Model), 使用者輸入驗證邏輯,檢視顯示邏輯,發起網路請求等程式碼的都放在viewModel中處理,從而大大的減少了viewController的程式碼量。
如果想深入瞭解他們的區別,就請移步自行Google吧,這裡就不贅述了。對於這些東西,只有自己寫過了,才能慢慢的從中體會到他們的差別,至於哪個好哪個適合自己,就只能自己從中取捨了。
下面講述的是一個結合了三方ReactiveObjC
,採用MVVM的構建模式結合RAC寫了一個登入的小功能,從demo中,能夠很清楚的看到,viewController中的程式碼量大大的減少了,至少達到了V瘦身的效果��。
首先要pod ReactiveObjC到專案當中
先來看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發射訊號,裡面處理一些網路請求
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;
}
//處理網路資料請求,請求成功之後跳轉
if ([weakself.loginView.usernameTextField.text isEqualToString:@"123"]&&[weakself.loginView.passwordTxtField.text isEqualToString:@"123"]) {
[[weakself getCurrentVC] showMessage:@"驗證成功之後跳轉到首頁" afterDelay:1.0 completion:^{
[[weakself getCurrentVC] presentViewController:[[HomeViewController alloc]init] animated:YES completion:nil];
}];
}else
{
[[weakself getCurrentVC] showMessage:@"賬戶名或密碼錯誤!賬戶名:123 密碼:123" afterDelay:2.0];
}
[subscriber sendCompleted];
return nil;
}];
}];
}
@end
最後就是viewController了,裡面只處理接收viewModel發過來的訊號,關聯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:@"點選了註冊按鈕" afterDelay:1.0];
}];
}
@end
viewController裡面50行程式碼不到就能搞定了一個登入頁。
RACCommand
和RACSignal
這兩個類裡面還有很多個方法,至於具體怎麼用可以直接百度Google。
簡書地址
demo傳送門,如果覺得喜歡的話,請給個星支援一下哦����