RAC textView的双向绑定

时间:2020-12-04 02:56:47

今天在写关于textView的数据绑定时原先写法是这样的:

1、 RACChannelTo(self,textView.text) = RACChannelTo(self,model.text);

2、 我在运行的时候看到log并未发现我的model.text随着键盘输入的内容改变而发生变化。但是我点击button时用代码修改model.text的值时代码改变的值却能同步到textView上面,后来在*上看到一个相似的issue,https://github.com/ReactiveCocoa/ReactiveCocoa/issues/1473,这个问题的描述是这样的 (

They're not the same.

self.valueTextField.rac_newTextChannel    sends values when you type in the text field, but not when you change the text in the text field from code.

RACChannelTo(self.uiTextField, text) sends values when you change the text in the text field from code, but not when you type in the text field.

RACChannelTo(self.valueTextField, text)= RACChannelTo(self.viewModel, value); (其实官方文档是这么说的:UIKit classes don't expose KVO-compliant properties UIKIt里面的很多控件本身不支持KVO,而ReactiveCocoa本身是基于KVO实现的,所以就会出现这种双向绑定不成功的现象,这时候就需要我们手动用信号,或者是rac提供的其他属性来做处理完成双向绑定的操作)

也就是说当使用键盘手动输入时你的以为有效写法: RACChannelTo(self,textView.text) = RACChannelTo(self,model.text);其实是无效的

于是乎提问者采用了另外一种方式:

RACChannelTerminal *channelTerminal = self.valueTextField.rac_newTextChannel;

RACChannelTerminal *channelTerminal2 = RACChannelTo(self.viewModel, value);

//@interface RACChannelTerminal : RACSignal <RACSubscriber>

[channelTerminal subscribe:channelTerminal2];

[channelTerminal2 subscribe:channelTerminal];

[RACObserve(self.viewModel, value) subscribeNext:^(id x) {

    NSLog(@"x = %@", x);
}]; 对于此问题的关键是代码改变值和键盘输入的信号是不一致的。(我只能这么说了)
由于使用代码对model到view这个方向的绑定是没问题的,所以我们只要在textView的text改变的信号中做一个手动的设置值(在subscribeNext中主动设置model对应的属性值就可以完成双向绑定了) 解决:在signal的subscribeNext的block中进行一次赋值 代码如下:

#import "ViewController.h"

#import <ReactiveCocoa/ReactiveCocoa.h>

@interface Model : NSObject

@property (nonatomic, strong) NSString *text;

@end

@implementation Model

@end

@interface ViewController ()

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) Model *model;

@property (nonatomic, copy) NSString *str;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 80, CGRectGetWidth(self.view.bounds), 300)];

self.textView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.textView];

self.model = [Model new];

//1、(监听model的变化将变化映射到textView)这种写法其实已经是双向绑定的写法了,但是由于是textView的原因只能绑定model.text的变化到影响textView.text的值的变化的这个单向通道

RACChannelTo(self,textView.text) = RACChannelTo(self,model.text);

//2、(监听View的变化将view的内容映射到model中)在这里对textView的text changed的信号重新订阅一下,以实现上面channel未实现的另外一个绑定通道.

@weakify(self)

[self.textView.rac_textSignal subscribeNext:^(id x) {

@strongify(self)

self.model.text = x;

NSLog(@"model text is%@",self.model.text);

}];

UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 480, 60, 40)];

resetBtn.backgroundColor = [UIColor yellowColor];

[resetBtn setTitle:@"reset" forState:UIControlStateNormal];

[self.view addSubview:resetBtn];

resetBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {

@strongify(self)

RACSignal *signal = [RACSignal return:input];

[signal subscribeNext:^(id x) {

self.model.text = @"reset yet";

NSLog(@"model text is%@",self.model.text);

}];

return signal;

}];

}

@end