ReactiveCocoa 监听Enabled和添加Command出错的处理方法
当我要控制一个按钮是否有效和添加按钮事件时
这样写会出错:
//当两个输入框有值的时候,按钮才有效
RAC(self.sendBtn, enabled) = [RACSignal combineLatest: @[self.passwordOldTxt.rac_textSignal,self.passwordTxt.rac_textSignal]
reduce:^id(NSString *passwordOld, NSString *password){
return @(passwordOld.length > 0 && password.length > 0);
}];
@weakify(self);
self.sendBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
@strongify(self);
NSLog(@"self.sendBtn.rac_command TouchUpInside %@", self.title);
return [RACSignal empty];
}];
因为ReactiveCocoa把Enabled和Command都整合了
所以要写为
@weakify(self);
self.sendBtn.rac_command = [[RACCommand alloc] initWithEnabled:
[RACSignal combineLatest: @[self.passwordOldTxt.rac_textSignal,self.passwordTxt.rac_textSignal]
reduce:^id(NSString *passwordOld, NSString *password){
return @(passwordOld.length > 0 && password.length > 0);
}] signalBlock:^RACSignal *(id input) {
@strongify(self);
NSLog(@"self.sendBtn.rac_command TouchUpInside %@", self.title);
return [RACSignal empty];
}];
Blog都是随笔,只当做笔记,不会有详细介绍,测试请慎重。。。
浙公网安备 33010602011771号