I'm trying to wrap head around Reactive Cocoa, everything works fine except RAC(self.windButton.enabled)
sections (at the bottom), reduce blocks never gets called. Would be glad if someone could point me my errors. The code is self-explaining, basically app looks like this:
我试着在活性可可周围包装,除了RAC(self.windButton.enabled)部分(在底部)之外,一切都很好,减少块永远不会被调用。如果有人能指出我的错误,我会很高兴的。代码是自解释的,基本上app是这样的:
Here is implementation
这是实现
#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);
}];
}
My problem is that buttons never get disabled.
我的问题是按钮永远不会被禁用。
2 个解决方案
#1
1
I had to use RACAbleWithStart
to pre-fill signal with initial values since combineLatest
method waits until every provided signal has some value.
我不得不使用RACAbleWithStart来预填充具有初始值的信号,因为combineLatest方法一直等到每个提供的信号都有一些值。
RACSignal *windingSignal = RACAbleWithStart(self.winding);
RACSignal *maxWindingSignal = RACAbleWithStart(self.maxWinding);
RACSignal *minWindingSignal = RACAbleWithStart(self.minWinding);
#2
#1
1
I had to use RACAbleWithStart
to pre-fill signal with initial values since combineLatest
method waits until every provided signal has some value.
我不得不使用RACAbleWithStart来预填充具有初始值的信号,因为combineLatest方法一直等到每个提供的信号都有一些值。
RACSignal *windingSignal = RACAbleWithStart(self.winding);
RACSignal *maxWindingSignal = RACAbleWithStart(self.maxWinding);
RACSignal *minWindingSignal = RACAbleWithStart(self.minWinding);