1. 便于理解的使用方法
- viewModel.h内容
@property (nonatomic, copy) NSString *username;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, strong) RACCommand *loginButtonCommand;
- viewModel.m内容
- (instancetype)init {
self = [super init];
if (self) {
RACSignal *usernameSingal = [RACObserve(self, username) map:^id _Nullable(id _Nullable value) {
if ([(NSString *)value length] > 6) {
return @(YES);
} else {
return @(NO);
}
}];
RACSignal *passwordSingl = [RACObserve(self, password) map:^id _Nullable(id _Nullable value) {
if ([(NSString *)value length] > 6) {
return @(YES);
} else {
return @(NO);
}
}];
RACSignal *loginSingl = [RACSignal combineLatest:@[usernameSingal, passwordSingl] reduce:^id (NSNumber *username, NSNumber *password){
return @([username boolValue] && [password boolValue]);
}];
self.loginButtonCommand = [[RACCommand alloc] initWithEnabled:loginSingl signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
return [self loginWithUserName:self.username password:self.password];
}];
}
return self;
}
- (RACSignal *)loginWithUserName:(NSString *)name password:(NSString *)passWord {
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:[NSString stringWithFormat:@"姓名:%@ 密码:%@",self.username, self.password]];
[subscriber sendCompleted];
return nil;
}];
}
- 控制器的.m内容
定义vm属性
@property (nonatomic, strong) LoginViewModel *viewModel;
绑定信号
self.LoginButton.rac_command = self.viewModel.loginButtonCommand;
RAC(self.viewModel, username) = self.usernameTextfield.rac_textSignal;
RAC(self.viewModel, password) = self.passwordTextfield.rac_textSignal;
登录点击事件
[[self.viewModel.loginButtonCommand executionSignals] subscribeNext:^(id _Nullable x) {
[x subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
}];
以上就是最简单的使用方法了
2. 结合猿题库网络请求框架
ViewMode内容
.End