ios - 2 RACCommands 以便一个被禁用而另一个正在执行,反之亦然

标签 ios objective-c reactive-cocoa

我如何创建 2 个 RACCommand,以便在另一个执行时禁用一个,并且反之亦然

像这样,

_prevTrackCommand = [[RACCommand alloc] initWithEnabled: [_nextTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];

_nextTrackCommand = [[RACCommand alloc] initWithEnabled: [_prevTrackCommand.executing not] signalBlock:^RACSignal *(id _) {}];

但此代码将不起作用,因为 _nextTrackCommand_prevTrackCommand 初始化时为 nil

最佳答案

您可以使用一个 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];
}];

关于ios - 2 RACCommands 以便一个被禁用而另一个正在执行,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43652352/

相关文章:

ios - 从一个 View 移动到另一个 iOS

ios - RAC 和单元重用 : putting deliverOn: in the right place?

swift - RAC 4 Swift 失败方法

ios - 如何在我的 'third party server' 上验证 GKLocalPlayer?

iOS Swift 3 Framework - 在 Storyboard 中无法访问

当某些 pod 文件包含在桥接 header 中包含的文件中时,iOS 项目将不会构建

ios - 如何将Signal转换为SignalProducer

ios - 在私有(private)队列上运行任务并返回回调

iphone - 将导航 Controller 添加到选项卡栏应用程序(以编程方式)

iphone - 如何检查 block 中传递的 void 函数是否为空?