Reactive Cocoa - 然后是自定义信号与UI信号

时间:2022-06-01 16:50:55

I'm starting to work with Reactive Cocoa so I've written a little test to see whether I understand then construct. The goal was to output text from a text field to the label only after some signal is sent. I've tried rac_signalForControlEvents(UIControlEvents.TouchUpInside) and VERY simple custom signal. Former does not even call then closure while the latter does work as intended. What is the problem here?

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

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
        }
}

1 个解决方案

#1


0  

From the docs for then:

从当时的文档:

Ignores all nexts from the receiver, waits for the receiver to complete, then subscribes to a new signal.

忽略接收器中的所有nexts,等待接收器完成,然后订阅新信号。

button.rac_signalForControlEvents does not complete (at least not on a button tap), thats why nothing happens.

button.rac_signalForControlEvents没有完成(至少没有按下按钮),这就是没有任何反应的原因。

Original answer by iv-mexx can be found at https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2671

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

#1


0  

From the docs for then:

从当时的文档:

Ignores all nexts from the receiver, waits for the receiver to complete, then subscribes to a new signal.

忽略接收器中的所有nexts,等待接收器完成,然后订阅新信号。

button.rac_signalForControlEvents does not complete (at least not on a button tap), thats why nothing happens.

button.rac_signalForControlEvents没有完成(至少没有按下按钮),这就是没有任何反应的原因。

Original answer by iv-mexx can be found at https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2671

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