
屏幕快照 2019-03-25 05.08.48 PM.png
自上至下把代码贴出来:
MVVMView.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVVMView : UIView
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UITextField *userName;
@property (nonatomic, strong) UITextField *passWord;
@end
NS_ASSUME_NONNULL_END
MVVMView.m:
@implementation MVVMView
-(instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self ui];
}
return self;
}
- (void)ui {
self.userName.frame = CGRectMake(60, 120, self.frame.size.width-120, 60);
self.passWord.frame = CGRectMake(60, CGRectGetMaxY(self.userName.frame) + 60, self.frame.size.width-120, 60);
self.loginBtn.frame = CGRectMake(60, CGRectGetMaxY(self.passWord.frame) + 60, self.frame.size.width-120, 60);
[self addSubview:self.userName];
[self addSubview:self.passWord];
[self addSubview:self.loginBtn];
}
- (UIButton *)loginBtn {
if (!_loginBtn) {
_loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_loginBtn.backgroundColor = [UIColor grayColor];
[_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
_loginBtn.selected = false;
[_loginBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
return _loginBtn;
}
- (UITextField *)userName {
if (!_userName) {
_userName = [[UITextField alloc] initWithFrame:CGRectZero];
_userName.backgroundColor = [UIColor grayColor];
_userName.placeholder = @"用户名";
}
return _userName;
}
- (UITextField *)passWord {
if (!_passWord) {
_passWord = [[UITextField alloc] initWithFrame:CGRectZero];
_passWord.backgroundColor = [UIColor grayColor];
_passWord.placeholder = @"密码";
}
return _passWord;
}
@end
MVVMVC.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVVMVC : UIViewController
@end
NS_ASSUME_NONNULL_END
MVVMVC.m
#import "MVVMView.h"
#import "MVVMViewModel.h"
#import "ReactiveObjC.h"
@interface MVVMVC ()
@property (nonatomic, strong, readwrite) MVVMView *mvvmView;
@property (nonatomic, strong, readwrite) MVVMViewModel *mvvmViewModel;
@end
@implementation MVVMVC
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.mvvmView];
[self racBind];
}
- (void)racBind {
RAC(self.mvvmViewModel, account) = self.mvvmView.userName.rac_textSignal;
RAC(self.mvvmViewModel, password) = self.mvvmView.passWord.rac_textSignal;
RAC(self.mvvmView.loginBtn, enabled) = self.mvvmViewModel.btnEnableSignal;
//4、数据请求成功
[self.mvvmViewModel.loginCommand.executionSignals.switchToLatest subscribeNext:^(id _Nullable x) {
NSLog(@"数据请求成功: %@",x);
}];
//3、准备工作都完成啦,现在在按钮点击的时候就执行command
[[self.mvvmView.loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
[self.mvvmViewModel.loginCommand execute:@{@"accound":self.mvvmView.userName.text,@"password":self.mvvmView.passWord.text}];
}];
}
- (MVVMView *)mvvmView {
if (!_mvvmView) {
_mvvmView = [[MVVMView alloc] initWithFrame:self.view.frame];
_mvvmView.backgroundColor = [UIColor whiteColor];
}
return _mvvmView;
}
- (MVVMViewModel *)mvvmViewModel {
if (!_mvvmViewModel) {
_mvvmViewModel = [[MVVMViewModel alloc] init];
}
return _mvvmViewModel;
}
@end
MVVMViewModel.h
#import <Foundation/Foundation.h>
#import "ReactiveObjC.h"
NS_ASSUME_NONNULL_BEGIN
@interface MVVMViewModel : NSObject
@property (nonatomic, strong) RACSignal *btnEnableSignal;
@property (nonatomic, copy) NSString *account;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, strong) RACCommand *loginCommand;
@end
NS_ASSUME_NONNULL_END
MVVMViewModel.m
#import "MVVMViewModel.h"
@interface MVVMViewModel()
@end
@implementation MVVMViewModel
- (instancetype)init {
if (self = [super init]) {
[self setUP];
}
return self;
}
- (void)setUP {
_btnEnableSignal = [RACSignal combineLatest:@[RACObserve(self,account),RACObserve(self, password)] reduce:^id _Nullable(NSString * account,NSString * password){
return @(account.length && (password.length > 5));
}];
_loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
NSLog(@"组合参数,准备发送登录请求 - %@", input);
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"开始请求");
NSLog(@"请求成功");
NSLog(@"处理数据");
[subscriber sendNext:@"请求后的数据"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"结束了");
}];
}];
return nil;
}];
}
@end