ReactiveCocoa使用心得
2020-04-06 本文已影响0人
freesan44
- 单向绑定
//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]]];
- 双向绑定
RACChannelTo(self.textField, text) = RACChannelTo(self.viewModel, someProperty)
- 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;
}];
- RACSubject
-(RACSubject *)openChargeRecordHandlerSbj
{
if (!_openChargeRecordHandlerSbj) {
_openChargeRecordHandlerSbj = [RACSubject subject];
@weakify(self)
[_openChargeRecordHandlerSbj subscribeNext:^(id _Nullable x) {
//TODO: 打开充值记录
}];
}
return _openChargeRecordHandlerSbj;
}
订阅
[self.viewModel.openChargeRecordHandlerSbj sendNext:nil];