我正在尝试将反应性可可粉包裹起来,除了RAC(self.windButton.enabled)
部分(在底部)之外,其他所有东西都工作正常,reduce块永远不会被调用。如果有人可以指出我的错误,我将很高兴。代码是不言自明的,基本上应用程序看起来像这样:
这是实现
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) NSNumber *winding;
@property (strong, nonatomic) NSNumber *minWinding;
@property (strong, nonatomic) NSNumber *maxWinding;
@property (weak, nonatomic) IBOutlet UIButton *windButton;
@property (weak, nonatomic) IBOutlet UIButton *unwindButton;
@property (weak, nonatomic) IBOutlet UILabel *windingLabel;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.winding = @0;
self.minWinding = @0;
self.maxWinding = @15;
RACSignal *windingSignal = RACAble(self.winding);
RACSignal *maxWindingSignal = RACAble(self.maxWinding);
RACSignal *minWindingSignal = RACAble(self.minWinding);
RAC(self.windingLabel.text) = [windingSignal map:^id(NSNumber *winding) {
return winding.stringValue;
}];
[[self.windButton rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(id sender) {
self.winding = @(self.winding.integerValue + 1);
}];
[[self.unwindButton rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(id sender) {
self.winding = @(self.winding.integerValue - 1);
}];
RAC(self.windButton.enabled) = [RACSignal
combineLatest:@[windingSignal, maxWindingSignal]
reduce:^(NSNumber *winding, NSNumber *maxWinding) {
return @(winding.intValue < maxWinding.intValue);
}];
RAC(self.unwindButton.enabled) = [RACSignal
combineLatest:@[windingSignal, minWindingSignal]
reduce:^(NSNumber *winding, NSNumber *minWinding) {
return @(winding.intValue > minWinding.intValue);
}];
}
我的问题是按钮永远不会被禁用。
最佳答案
我不得不使用RACAbleWithStart
来用初始值预填充信号,因为combineLatest
方法会一直等到每个提供的信号都有某个值。
RACSignal *windingSignal = RACAbleWithStart(self.winding);
RACSignal *maxWindingSignal = RACAbleWithStart(self.maxWinding);
RACSignal *minWindingSignal = RACAbleWithStart(self.minWinding);