Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get a signal every time a UITextField text property changes in ReactiveCocoa 5

How do you get a signal from both user-initiated and programmatically made changes to UITextField text property? By using continuousTextValues only reports a signal when the user has initiated the change. If you set textField.text programmatically, the signal doesn't fire.

This is how I'm using continuousTextValues:

textField.reactive.continuousTextValues.observeValues { value in
    print("Value: \(value)")
}

It doesn't get triggered if I set text manually:

textField.text = "Test"
like image 242
Markus Rautopuro Avatar asked Jan 05 '17 15:01

Markus Rautopuro


1 Answers

The signal continuousTextValueswill only be triggered while user input using the keyboard.You could try this:

var characters = MutableProperty("")

tf.reactive.text <~ characters
tf.reactive.continuousTextValues.observeValues { [weak characters = characters] (text) in
   characters?.value = text!
}
tf.reactive.textValues.observeValues { [weak characters = characters] (text) in
   characters?.value = text!
}

characters.producer.skip(while: { $0.isEmpty }).startWithValues { (text) in
   log.debug("text = \(text)")
}

characters.value = "shaw"
like image 98
Shaw Avatar answered Sep 28 '22 02:09

Shaw