gpt4 book ai didi

ios - 将 rx.value 添加到我的 CustomView

转载 作者:搜寻专家 更新时间:2023-10-31 22:09:46 25 4
gpt4 key购买 nike

假设我有一个 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})
}

}

关于ios - 将 rx.value 添加到我的 CustomView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50970117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com