gpt4 book ai didi

ios - 如何使用 RxSwift 观察 UITextField 中的文本变化?

转载 作者:可可西里 更新时间:2023-11-01 00:38:47 26 4
gpt4 key购买 nike

我想使用 RxSwift 观察 UITextField.text 属性的任何变化。

我尝试使用 rx.text 属性,但它仅在我设置 text 属性时调用,如下所示:textField.text = "",但在我输入文本时不起作用。

我也尝试使用 rx.observe(String.self, "text"),但它给出了相同的结果。

如何观察 UITextField 中文本的任何变化?

最佳答案

要观察 UITextField 中文本的任何变化,您可以为 Reactive 类创建扩展:

public extension Reactive where Base: UITextField {

public var textChange: Observable<String?> {
return Observable.merge(self.base.rx.observe(String.self, "text"),
self.base.rx.controlEvent(.editingChanged).withLatestFrom(self.base.rx.text))
}

}

关于ios - 如何使用 RxSwift 观察 UITextField 中的文本变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52983561/

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