0

我开始使用 Reactive Cocoa,所以我写了一个小测试,看看我是否理解然后构造。目标是仅在发送某些信号后才将文本从文本字段输出到标签。我试过 rac_signalForControlEvents(UIControlEvents.TouchUpInside) 和非常简单的自定义信号。前者甚至不调用then闭包,而后者确实按预期工作。这里有什么问题?

func someSetupFunction()
{
#if true
    // Why does this doesn't work?

    button.rac_signalForControlEvents(UIControlEvents.TouchUpInside).then
        {
            let strongSelf = weakSelf
            return strongSelf?.textField.rac_textSignal()
        }.subscribeNext
        { object in
            let strongSelf = weakSelf
            strongSelf?.label.text = object as! String! + " - 0"
        }
#else
    // ... but this does?

    customSignal().then
        {
            let strongSelf = weakSelf
            return strongSelf?.textField.rac_textSignal()
        }.subscribeNext
        { object in
            let strongSelf = weakSelf
            strongSelf?.label.text = object as! String! + " - 1"
        }
#endif
}

func customSignal() -> RACSignal
{
    return RACSignal.createSignal
        { subscriber in
            subscriber.sendNext(nil)
            subscriber.sendCompleted()
            return nil
        }
}
4

1 回答 1

0

来自当时的文档:

忽略接收者的所有下一步,等待接收者完成,然后订阅新信号。

button.rac_signalForControlEvents 没有完成(至少没有点击按钮),这就是为什么什么也没发生。

iv-mexx的原始答案可以在https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2671找到

于 2016-01-20T12:27:29.810 回答