zoukankan      html  css  js  c++  java
  • iOS开发ReactiveCocoa学习笔记(三)

    RAC常用用法:

    1.监听按钮的点击事件:

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 100, 80, 40);
        [button setTitle:@"点击事件" forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:button];
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSLog(@"按钮被点击了");
        }];

    2.代理的使用:

    @interface RedView : UIView
    
    //*  */
    @property (nonatomic, strong) RACSubject *btnClickSignal;
    
    @end
    #import "RedView.h"
    
    @implementation RedView
    - (void)awakeFromNib {
    
        [super awakeFromNib];
    }
    
    //懒加载信号
    - (RACSubject *)btnClickSignal {
    
        if (_btnClickSignal == nil) {
            _btnClickSignal = [RACSubject subject];
        }
        return _btnClickSignal;
    }
    
    - (IBAction)btnClick:(UIButton *)sender {
        
        [self.btnClickSignal sendNext:@"按钮被点击了"];
    }
    
    @end
     // 只要传值,就必须使用RACSubject
        RedView *redView = [[[NSBundle mainBundle] loadNibNamed:@"RedView" owner:nil options:nil] lastObject];
        redView.frame = CGRectMake(0, 140, self.view.bounds.size.width, 200);
        [self.view addSubview:redView];
        [redView.btnClickSignal subscribeNext:^(id x) {
            NSLog(@"---点击按钮了,触发了事件");
        }];
        
        // 把控制器调用didReceiveMemoryWarning转换成信号
        //rac_signalForSelector:监听某对象有没有调用某方法
    
        [[self rac_signalForSelector:@selector(didReceiveMemoryWarning)] subscribeNext:^(id x) {
            NSLog(@"控制器调用didReceiveMemoryWarning");
    
        }];

    3.KVO

    把监听redVcenter属性改变转换成信号,只要值改变就会发送信号

     // observer:可以传入nil
        [[redView rac_valuesAndChangesForKeyPath:@"center" options:NSKeyValueObservingOptionNew observer:nil] subscribeNext:^(id x) {
            NSLog(@"center:%@",x);
        }];

    4.代替通知

    UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(100, 340, 150, 40)];
        textfield.placeholder = @"监听键盘的弹起";
        textfield.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:textfield];
        
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) {
            NSLog(@"监听键盘的高度:%@",x);
        }];
        

    5.监听文字的改变

       [textfield.rac_textSignal subscribeNext:^(id x) {
           
            NSLog(@"输入框中文字改变了---%@",x);
        }];
        

    6.处理多个请求,都返回结果的时候,统一做处理.

     RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            // 发送请求1
            [subscriber sendNext:@"发送请求1"];
            return nil;
        }];
        
        RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            // 发送请求2
            [subscriber sendNext:@"发送请求2"];
            return nil;
        }];
        
        // 使用注意:几个信号,参数一的方法就几个参数,每个参数对应信号发出的数据。
        [self rac_liftSelector:@selector(updateUIWithR1:r2:) withSignalsFromArray:@[request1,request2]];
    
    
    // 更新UI
    - (void)updateUIWithR1:(id)data r2:(id)data1
    {
        NSLog(@"更新UI%@  %@",data,data1);
    }

     

  • 相关阅读:
    我工作三年了,该懂并发了!
    代理,一文入魂
    非典型算法题,用程序和电脑玩一个游戏
    详解command设计模式,解耦操作和回滚
    matplotlib画图教程,设置坐标轴标签和间距
    详解工程师不可不会的LRU缓存淘汰算法
    详解深度学习感知机原理
    详解gitignore的使用方法,让你尽情使用git add .
    算法题 | 你追我,如果你追到我……那就算你赢了
    险些翻车,差一点没做出来的基础算法题
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/6803927.html
Copyright © 2011-2022 走看看