1.RACSignal *validUsernameSignal = [self.usernameTextField.rac_textSignal map:^id(NSString *text) { return @([self isValidUsername:text]); }];
RACSignal *validPasswordSignal = [self.passwordTextField.rac_textSignal map:^id(NSString *text) { return @([self isValidPassword:text]); }];
创建有效状态信号,map起到的作用就是做判断,如果是有效的输入则返回值,否则就不返回
2.RAC(self.passwordTextField, backgroundColor) =
[validPasswordSignal map:^id(NSNumber *passwordValid){
return[passwordValid boolValue] ? [UIColor clearColor]:[UIColor yellowColor];
}];
RAC(self.usernameTextField, backgroundColor) =
[validUsernameSignal map:^id(NSNumber *passwordValid){
return[passwordValid boolValue] ? [UIColor clearColor]:[UIColor yellowColor];
}];
进一步改善代码,map做判断,为真返回clearcolor,假则返回yellowcolor[objc]
3.[[validPasswordSignal map:^id(NSNumber *passwordValid){
return[passwordValid boolValue] ? [UIColor clearColor]:[UIColor yellowColor];
}]
subscribeNext:^(UIColor *color){
self.passwordTextField.backgroundColor = color;
}];
这是完整的代码,map接收输入的text,做判断,真就返回clearcolor,假则返回yellowcolor,subscribeNext则将接收到的color作为textfield的背景颜色