RxSwift:双向绑定 [英] RxSwift: Two way binding
本文介绍了RxSwift:双向绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了官方双向绑定解决方案
func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable{
let bindToUIDisposable = variable.asObservable()
.bindTo(property)
let bindToVariable = property
.subscribe(onNext: { n in
variable.value = n
}, onCompleted: {
bindToUIDisposable.dispose()
})
return Disposables.create(bindToUIDisposable, bindToVariable)
}
用法:(textField.rx.text< - > object.property).addDisposableTo(disposeBag)
属性定义: var property = Variable< String?>(没有)
- 在onNext方法中所有ok和
变量
更改了它的值,但我的object.property
没有改变。 - 有没有办法将变量当前值设置为ControlProperty在< - >方法中,bcs我需要在订阅开始之前设置初始值吗?
- In onNext method all ok and
variable
changed its value, but myobject.property
doesn't changed. - Is there any way to set variable current value into ControlProperty inside of <-> method, bcs I need set initial value, before subscribe starts?
推荐答案
<我的错。我在绑定后用另一个实例替换了对象
My fault. I replaced object with another instance after binding
此代码运行良好,控制属性从变量接收初始值
This code works well and control property receive initial value from variable
这篇关于RxSwift:双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文