简体   繁体   中英

textColor with ReactiveCocoa

when instead a textField longer the firstResponder, does not send any signal value and the text color is not correct, how can I fix it?

    RAC(self.textField, textColor) = [RACSignal
                                  combineLatest:@[self.textField.rac_textSignal]
                                         reduce:^(NSString *firstName) {
                                              if (firstName.length > 5) {
                                                  return [UIColor blueColor];
                                              }else{
                                                 return [UIColor redColor];
                                              }
                                  }];

Assuming you mean this to show the validation state of a field you can do it like this. There are tons of other ways but this is the one that I normally use.

- (void)viewDidLoad {
    [super viewDidLoad];

    // =====================================
    // Other Initialisation Code Here
    // =====================================

    RACSignal *validTextFieldSignal =
        [self.textField.rac_textSignal
            map:^id(NSString *text) {
                return @([self isValidTextField:text]);
            }];


     RAC(self.textField, textColor) =
                [validTextFieldSignal
                    map:^id(NSNumber *textFieldValid) {
                        return [textFieldValid boolValue] ? [UIColor blueColor] : [UIColor redColor];
                    }];
end

- (BOOL)isValidTextField:(NSString *)textField {
    return textField.length > 5;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM