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];
                                }];

  

posted @ 2015-12-03 11:02  张永存(Jerry)  阅读(402)  评论(0编辑  收藏  举报