1.单向绑定
//model->viewmodel的model
RAC(self.viewModel,model) = RACObserve(self, model);
在cell中对控件进行绑定,需要添加takeUntil
//注意,因为cell复用问题,所以要在每个UI控件上添加一个takeUntil:self.rac_prepareForReuseSignal
RAC(self.viewModel.model,MobileNo) = [RACSignal merge:@[[self.phoneNumTextField.rac_textSignal takeUntil:self.rac_prepareForReuseSignal],[RACObserve(self.phoneNumTextField, text) takeUntil:self.rac_prepareForReuseSignal]]];
2.双向绑定
RACChannelTo(self.textField, text) = RACChannelTo(self.viewModel, someProperty)
3.RACCommand
-(RACCommand *)openContactManagerCommand
{
if (!_openContactManagerCommand) {
_openContactManagerCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
DebugLog(@"%@",input);
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[[LJContactManager sharedInstance] selectContactAtController:Context.rootNavViewController complection:^(NSString *name, NSString *phone) {
[subscriber sendNext:phone];
[subscriber sendCompleted];
}];
return nil;
}];
}];
}
return _openContactManagerCommand;
}
订阅:
[[self.viewModel.openContactManagerCommand execute:nil]subscribeNext:^(NSString* _Nullable x) {
self.phoneNumTextField.text = x;
}];
4.RACSubject
-(RACSubject *)openChargeRecordHandlerSbj
{
if (!_openChargeRecordHandlerSbj) {
_openChargeRecordHandlerSbj = [RACSubject subject];
@weakify(self)
[_openChargeRecordHandlerSbj subscribeNext:^(id _Nullable x) {
//TODO: 打开充值记录
}];
}
return _openChargeRecordHandlerSbj;
}
订阅
[self.viewModel.openChargeRecordHandlerSbj sendNext:nil];
5.NSMutableArray 因为NSMutableArray不支持KVO,所以用另外一个方式处理:
[[self rac_valuesAndChangesForKeyPath:@"modelArr" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld observer:nil]subscribeNext:^(RACTwoTuple<id,NSDictionary *> * _Nullable x) {
@strongify(self)
NSMutableArray * arr = x.first;
NSDictionary * changeDic = x.second;
DebugLog(@"RAC---%@",changeDic);
[self reloadCollectionView];
}];
添加对象要用KVO
[[self mutableArrayValueForKey:@"modelArr"]addObject:model];