ios - How to respond to programmatic changes of a TextView text -
the textview delegate set:
textview.delegate = self //self being uitextviewdelegate
but delegate method doesn't called when text set programmatically
func textviewdidchange(_ textview: uitextview) { print(textview.text) }
how respond text changes without going reactive?
i tried kvo on uitextview,
self.textview1.text = "you working, change in 5 seconds"
add observer
self.textview1.addobserver(self, forkeypath: "text", options: nskeyvalueobservingoptions(rawvalue: 0), context: nil)
trigger text change programmatically, example way want.
dispatchqueue.main.asyncafter(deadline:.now() + 5) { self.textview1.text = "changed after time" }
override kvo method.
override func observevalue(forkeypath keypath: string?, of object: any?, change: [nskeyvaluechangekey : any]?, context: unsafemutablerawpointer?) { if object == self.textview1{ //do thing here... } }
cool works! kvo on uikit works indeed state in apple docs below, fyi.
note: although classes of uikit framework not support kvo, can still implement in custom objects of application, including custom views.
Comments
Post a Comment