如何创建2个RACCommand
以便在执行其他操作时禁用其中一个{strong>反之亦然?
像这样,
_prevTrackCommand = [[RACCommand alloc] initWithEnabled: [_nextTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];
_nextTrackCommand = [[RACCommand alloc] initWithEnabled: [_prevTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];
但是此代码无效,因为_nextTrackCommand
nil
已被_prevTrackCommand
启用。
答案 0 :(得分:1)
您可以使用可以作为RACSubject
使用的RACSignal
,但允许您手动转发事件:
RACSubject* commandAActive = [RACSubject subject];
RACSubject* commandBActive = [RACSubject subject];
RACCommand* commandA = [[RACCommand alloc] initWithEnabled:[commandBActive not]
signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
// block
}];
RACCommand* commandB = [[RACCommand alloc] initWithEnabled:[commandAActive not]
signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
// block
}];
[commandA.executing subscribeNext:^(NSNumber * _Nullable x) {
[commandAActive sendNext:x];
}];
[commandB.executing subscribeNext:^(NSNumber * _Nullable x) {
[commandBActive sendNext:x];
}];