RAC的学习
2017-04-08 本文已影响31人
EdenChow
创建一个信号 (最简单RACSignal信号)
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendCompleted];
return nil;
}] ;
[signal subscribeNext:^(NSString * _Nullable x) {
NSLog(@"---%@---",x);
}];
/// 或者 (UI控件状态绑定信号)
RAC(self.loginButton,enabled) = signal;
信号的聚合
RACSignal *signal = [RACSignal combineLatest:
@[ _accountTextField.rac_textSignal,
_passwordTextField.rac_textSignal
]
reduce:^id (NSString *account, NSString *password){
return @(account.length && password.length);
}];
[signal subscribeNext:^(id _Nullable x) {
self.loginButton.enabled = [x boolValue];
}];
RAC (宏)
RAC(self.titleLable,text) = self.accountTextField.rac_textSignal; // 控件绑定信号
KVC (属性监听)
RAC(self.titleLable,text) = self.accountTextField.rac_textSignal; // 绑定
/// KVO
[[RACObserve(self.titleLable, text) filter:^BOOL(NSString * _Nullable value) {
return value.length > 0; // 有文字才执行
}] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
}];