Manually setting “text” for a UITextField does not call .EditingChanged

萝らか妹 提交于 2019-12-13 05:48:44

问题


I have subclassed UITextField which has the target

self.addTarget(self, action: "onChange:", forControlEvents: .EditingChanged)

This works fine when the user is typing in the textfield. Although I would need to the same method "onChange:" to be called when the textfield's "text" property is manually updated.

let tf = CustomTextField()
tf.text = "Trigger !!!"

How could I do this ?


回答1:


This is intentional, to prevent endless echo if you need to change the text programmatically in an event handler.

Use sendActions(for controlEvents:) to manually notify event handlers.

tf.text = "Trigger !!!"
tf.sendActions(for: .editingChanged)


来源:https://stackoverflow.com/questions/35791976/manually-setting-text-for-a-uitextfield-does-not-call-editingchanged

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!