ReactiveCocoa进阶(1)

ReactiveCocoa的实际应用

1.监听方法执行
  • 方法:rac_signalForSelector
[[v rac_signalForSelector:@selector(touchesBegan:withEvent:)] subscribeNext:^(id  _Nullable x) {
    NSLog(@"点击了我");
 }];
  • 这个方法是创建对象中一定会调用的,所以在此方法中可以监听各个系统方法的执行,换了一种方式来写各个系统方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    ViewController *vc = [super allocWithZone:zone];
    // ViewDidLoad
    [[vc rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id  _Nullable x) {
        NSLog(@"ViewDidLoad");
    }];
    // ViewWillAppear
    [[vc rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id  _Nullable x) {
        NSLog(@"ViewWillAppear");
    }];
    return vc;
}
2.监听按钮点击
  • 方法:rac_signalForControlEvents
[[_button rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(id x) {
}];
3.监听文字的改变 :(替代textField的代理)
  • 方法:rac_textSignal
[_passWordTextField.rac_textSignal subscribeNext:^(id x) {
    _passWordLabel.text = x;
}];
  • 方法:宏定义 RAC() 信号绑定
RAC(_passWordLabel, text) = _passWordTextField.rac_textSignal;
  • 错误示范
//这样并不能获取textField的文字信号,获取的是textField本身的信号,写法错误
[[_passWordTextField rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(id x) {
    NSLog(@"%@", x);
    _passWordLabel.text = x; 
}];
4.代替KVO
  • 方法:rac_valuesForKeyPath:@keypath(self, age) observer:self
    //方法默认监听新值和旧值
    [[self rac_valuesForKeyPath:@keypath(self, age) observer:self] subscribeNext:^(id x) {
        NSLog(@"0 - age = %d", self.age);
    }];
  • 方法:rac_valuesAndChangesForKeyPath:@"age" options:NSKeyValueObservingOptionNew observer:self
    //按照选项监听新值或是旧值
    [[self rac_valuesAndChangesForKeyPath:@"age" options:NSKeyValueObservingOptionNew observer:self] subscribeNext:^(id x) {
        NSLog(@"1 - age = %d", self.age);
    }];
  • 方法:rac_observeKeyPath:@"age" options:NSKeyValueObservingOptionNew observer:self block:
    //按照选项监听新值或是旧值
    [self rac_observeKeyPath:@"age" options:NSKeyValueObservingOptionNew observer:self block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
        NSLog(@"2 - age = %d", self.age);
    }];
  • 方法:宏定义RACObserve()
//方法默认监听新值和旧值
[RACObserve(self, age) subscribeNext:^(id x) {
    NSLog(@"3 - age = %d", self.age);
}];
5.代替通知
  • 方法:rac_addObserverForName
//不需要手动管理(移除)观察者
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"Note" object:nil] subscribeNext:^(id x) {
    NSLog(@"%@", x);
}];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Note" object:nil];
6.当某几个信号执行完毕再去调用一个方法(rac_liftSelector)
  • 注意:有几个信号就必须有几个参数,参数就是订阅者发出的数据
    // 创建请求最热数据信号
    RACSignal *hotSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        // 请求最热数据
        [subscriber sendNext:@"最热数据"];
        return nil;
    }];
    // 创建请求最新数据信号
    RACSignal *newSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        // 请求最新的数据
        [subscriber sendNext:@"最新数据"];
        return nil;
    }];
    // Selector:当数组所有信号都发送next之后调用
    [self rac_liftSelector:@selector(updateUIWithHot:new:) withSignalsFromArray:@[hotSignal,newSignal]];

你可能感兴趣的:(ReactiveCocoa进阶(1))