God*_*her 3 ios swift rx-swift rx-cocoa
假设我有一个带有值的CustomView.我想使用rx.value(Observable)将该值公开给世界,而不是必须通过值(Int)访问它.
final class CustomView: UIView {
var value: Int = 0
...
}
Run Code Online (Sandbox Code Playgroud)
我从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
}
Run Code Online (Sandbox Code Playgroud)
但customView.rx.value不会发出任何值
小智 6
缺少的是,你需要发送动作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})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1189 次 |
| 最近记录: |