每当RxSwift中的UITextField文本属性更改时,如何获得信号 [英] How do you get a signal every time a UITextField text property changes in RxSwift

查看:77
本文介绍了每当RxSwift中的UITextField文本属性更改时,如何获得信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过编程方式对UITextField文本属性进行更改来获得信号?通过使用rx.text仅在用户通过键盘输入文本时报告信号.如果您以编程方式设置textField.text,则不会触发信号.

How do you get a signal from programmatically made changes to UITextField text property? By using rx.text only reports a signal when the user input the text by keyboard. If you set textField.text programmatically, the signal doesn't fire.

这就是我使用rx.text的方式

This is how I'm using rx.text

(self.phoneNumberTextField.rx.text).orEmpty.subscribe(onNext: { [weak self] (phone) in
         // do something
    }).addDisposableTo(disposeBag)
    self.phoneNumberTextField.text = "this is for test"

谢谢!

推荐答案

我遇到了同样的问题,显然清扫程序的回答对我不起作用.所以这就是我所做的手动设置文本字段的文本时,我在文本字段上调用sendActions方法

I had the same issues, apparently sweeper's answer did not work for me. So here is what I did When I set the text for textfield manually, I call sendActions method on the text field

textField.text =以编程方式设置文本.textField.sendActions(for:.valueChanged)

在深入研究时,我意识到 rx.text 取决于 UIControlEvents 并且当您显式设置文本时不会触发这些操作.

On digging a little bit more, I realized that the rx.text depends on UIControlEvents and these are not triggered when you explicitly set the text.

希望这会有所帮助

这篇关于每当RxSwift中的UITextField文本属性更改时,如何获得信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆