一、导图
二、具体实现
具体代码:https://github.com/lionsom/LXReactiveCocoa_Demo
1、代替代理 : RACSubject
场景:View -> Controller 进行传值
- (void)demo_1 {
//1、RAC代替代理 : RACSubject
//原理:监听greenview中的方法调用btnOnclick
[[_GreenView rac_signalForSelector:@selector(btnOnclick:)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"AAAAAA == %@",x);
}];
[[_GreenView rac_signalForSelector:@selector(PassValue::)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"AAAAAA == %@ == %@ == %@",x,x[0],x[1]);
}];
}
2、代替监听KVO
- (void)demo_2 {
//2、代替监听KVO
//方法一:
// observer 这里可以直接为 nil
[_GreenView rac_observeKeyPath:@"frame" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
//回调
NSLog(@"变化了 Value = %@ ; Change = %@",value,change);
}];
//方法二:
// 前面是创建信息,然后直接订阅这个信号
[[_GreenView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id _Nullable x) {
NSLog(@"BBBBBB == %@",x);
}];
}
3、监听事件
- (void)demo_3 {
//3、监听事件
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//打印出的就是按钮的本身
NSLog(@"CCCC == %@",x);
}];
}
4、代替通知
-(void)demo_4 {
//4、代替通知
//键盘弹出 通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"DDDDD == %@",x);
}];
}
5、监听文本框
-(void)demo_5 {
//5、监听文本框
// * 文本信号 *
[_textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
NSLog(@"EEEEE == %@",x);
}];
}