[英]How to bind Signal<Bool, NoError> to enabled property of UIButton in Reactive Cocoa 4
I have a very simple use case for ReactiveCocoa
. 我有一个非常简单的ReactiveCocoa
用例。 In my setup, I have two UITextField
s and one UIButton
. 在我的设置中,我有两个UITextField
和一个UIButton
。
The button's enabled
property should only be set to true
iff both textfields contain at least one character. 如果两个文本字段都包含至少一个字符,则按钮的enabled
属性应仅设置为true
。
I started out creating a Signal<Bool, NoError>
that emits true
or false
depending on the the above mentioned condition. 我开始根据上面提到的条件创建一个发出true
或false
的Signal<Bool, NoError>
。
Now, as far as I understand I somehow need to bind the signal (or its values) to the enabled
property of my button. 现在,据我所知,我不知何故需要将信号(或其值) 绑定到我的按钮的enabled
属性。 But I have no clue how to do this and more than an hour of research haven't lead to any results... 但我不知道如何做到这一点,超过一个小时的研究没有导致任何结果......
UPDATE: From what I understand, this could previously be achieved using the RAC
macro: RAC(self.button, enabled) = signal;
更新:根据我的理解,以前可以使用RAC
宏实现: RAC(self.button, enabled) = signal;
, or something along those lines. , 或类似的规定。 But that doesn't help me since the macros have been deprecated in RAC 3. My question is basically the same as this one only for RAC 4 instead of 3. 但是,这并不能帮助我,因为宏已经在RAC 3.被弃用我的问题是基本相同, 这一个只为RAC 4,而不是3。
This can be achieved using the custom <~
operator. 这可以使用自定义<~
运算符来实现。 However, it only works on properties of type MutableProperty
, so we can't just do the following: 但是,它只适用于MutableProperty
类型的MutableProperty
,因此我们不能只执行以下操作:
let signal: <Bool, NoError> = ...
button.enabled <~ signal
Instead, we need to wrap the button's enabled
property in a MutableProperty
like so: 相反,我们需要将按钮的enabled
属性包装在MutableProperty
如下所示:
extension UIButton {
public var rac_enabled: MutableProperty<Bool> {
return lazyMutableProperty(self, key: &AssociationKey.text, setter: { self.enabled = $0 }, getter: { self.enabled })
}
}
Note that this implementation depends on this gist which was created by Colin Eberhardt . 请注意,此实现取决于Colin Eberhardt创建的这个要点 。
Now we can just do: 现在我们可以这样做:
button.rac_enabled <~ signal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.