我想使用 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/

10-12 00:15
查看更多