2 RACCommands,以便在执行其他操作时禁用其中一个,反之亦然

时间:2017-04-27 08:20:25

标签: ios objective-c reactive-cocoa

如何创建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启用。

1 个答案:

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