ReactiveCocoa学习(一)

前端之家收集整理的这篇文章主要介绍了ReactiveCocoa学习(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、监听文本框值的改变

UITextField *tfd=[[UITextField alloc]initWithFrame:CGRectMake(50,50,200,30)];
    tfd.borderStyle=UITextBorderStyleRoundedRect;
    tfd.placeholder=@"请输入您的姓名";
    tfd.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:tfd];
    //监听文本框值的改变
    [tfd.rac_textSignal subscribeNext:^(id x) {
       //打印文本框输入的值
        NSLog(@"%@",x);
    }];

2、对文本框内容进行过滤,过滤掉长度小于3的内容

//****************& 例一 &****************//

    [[tfd.rac_textSignal filter:^BOOL(id value) {
        NSString *textvalue=value;
        return textvalue.length>3;
     }]

      subscribeNext:^(id x) {
          NSLog(@"%@",x);
      }
    ];
       //或者写成这样
     //****************& 例二 &****************//
    RACSignal *tfdSingale=tfd.rac_textSignal;
    RACSignal *tfdSingfilter=[tfdSingale filter:^BOOL(id value) {
        NSString *textvalue=value;
        return textvalue.length>3;
    }];
    [tfdSingfilter subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];

3、 按文本长度过滤

[[[tfd.rac_textSignal
      map:^id(id value) {
          NSString *textvalue=value;
          return @(textvalue.length);
      }]
      filter:^BOOL(id value) {
          NSInteger numvalue=[value integerValue];
          return (numvalue>3);
      }]
      subscribeNext:^(id x) {
          NSLog(@"%@",x);
      }];
//判断是否是有效的值
    RACSignal *validUsernameSignal =
    [tfd.rac_textSignal
     map:^id(NSString *text) {
         return @([self isValidUsername:text]);
     }];

    //打印结果
    [validUsernameSignal subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];

4、根据文本框的值改变文本框的背景图

[[tfd.rac_textSignal
     map:^id(NSNumber *text) {
         return [text boolValue]?[UIColor clearColor]:[UIColor yellowColor];
     }]
      subscribeNext:^(UIColor * color) {
          tfd.backgroundColor=color;
    }];

利用ReactiveCocoa提供的宏设置

RAC(tfd,backgroundColor)=[tfd.rac_textSignal map:^id(id value) {
        return [value boolValue]?[UIColor clearColor]:[UIColor yellowColor]
        ;
    }];

练习1。
根据用户名和密码判断按钮是否可用

UITextField *tfd=[[UITextField alloc]initWithFrame:CGRectMake(50,30)];
    tfd.borderStyle=UITextBorderStyleRoundedRect;
    tfd.placeholder=@"请输入您的姓名";
    tfd.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:tfd];

    UITextField *tfd2=[[UITextField alloc]initWithFrame:CGRectMake(50,100,30)];
    tfd2.borderStyle=UITextBorderStyleRoundedRect;
    tfd2.placeholder=@"请输入您的密码";
    tfd2.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:tfd2];

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(50,150,30)];
    [btn setTitle:@"点我啊" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];


    RACSignal *useSingle=[tfd.rac_textSignal map:^id(NSString * value) {

        return @([self isValidUsername:value]);
    }];

    RACSignal *pwdSingle=[tfd2.rac_textSignal map:^id(NSString * value) {

        return @([self isValidPassword:value]);
    }];

    RAC(tfd,backgroundColor)=[useSingle map:^id(id value) {
        return [value boolValue]?[UIColor clearColor]:[UIColor yellowColor]
        ;
    }];

    RAC(tfd2,backgroundColor)=[pwdSingle map:^id(id value) {
        return [value boolValue]?[UIColor clearColor]:[UIColor yellowColor]
        ;
    }];

    //把信号两个信号连接起来,判断按钮的状态
    RACSignal *btnsingle=[RACSignal combineLatest:@[useSingle,pwdSingle] reduce:^id(NSNumber *uservalid,NSNumber *pwdvalid){
        return @([uservalid boolValue]&&[pwdvalid boolValue]);
    }];

    [btnsingle subscribeNext:^(id x) {
        btn.enabled=[x boolValue];
        btn.backgroundColor=[x boolValue]?[UIColor redColor]:[UIColor grayColor];
    }];
原文链接:https://www.f2er.com/react/307391.html

猜你在找的React相关文章