可以说我有一个带有值的CustomView。我想使用rx.value(Observable)向世界展示该值,而不是必须通过值(Int)来访问它。
final class CustomView: UIView {
var value: Int = 0
...
}
我从UIStepper + Rx复制了此代码:
extension Reactive where Base: CustomView {
var value: ControlProperty<Int> {
return base.rx.controlProperty(editingEvents: [.allEditingEvents, .valueChanged],
getter: { customView in
customView.currentValue
}, setter: { customView, value in
customView.currentValue = value
}
)
}
}
final class CustomView: UIControl {
fileprivate var currentValue = 1 {
didSet {
checkButtonState()
valueLabel.text = currentValue.description
}
}
// inside i set currentValue = 3
}
但是customView.rx.value不会发出任何值
缺少的是,您需要在发送操作UIControl
。检查下一个示例:
class CustomView: UIControl {
var value: Int = 0 {
didSet { sendActions(for: .valueChanged) } // You are missing this part
}
}
extension Reactive where Base: CustomView {
var value: ControlProperty<Int> {
return base.rx.controlProperty(editingEvents: UIControlEvents.valueChanged,
getter: { customView in
return customView.value },
setter: { (customView, newValue) in
customView.value = newValue})
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句