@dengApro's answer is very close.
The source code in UITextField+Rx.swift
:
/// Reactive wrapper for `text` property.
public var value: ControlProperty<String?> {
return base.rx.controlPropertyWithDefaultEvents(
getter: { textField in
textField.text
},
setter: { textField, value in
// This check is important because setting text value always clears control state
// including marked text selection which is imporant for proper input
// when IME input method is used.
if textField.text != value {
textField.text = value
}
}
)
}
Assigning textField
a value could not be subscribed, because ofcontrolPropertyWithDefaultEvents
The source code in UIControl+Rx.swift
:
/// This is a separate method to better communicate to public consumers that
/// an `editingEvent` needs to fire for control property to be updated.
internal func controlPropertyWithDefaultEvents<T>(
editingEvents: UIControl.Event = [.allEditingEvents, .valueChanged],
getter: @escaping (Base) -> T,
setter: @escaping (Base, T) -> Void
) -> ControlProperty<T> {
return controlProperty(
editingEvents: editingEvents,
getter: getter,
setter: setter
)
}
So just the two events UIControl.Event = [.allEditingEvents, .valueChanged]
can be observable,
Variable changed, Variable bind to ControlProperty, ControlProperty changed not because of [.allEditingEvents, .valueChanged]
, then done.
ControlProperty changed, ControlProperty bind to Variable, Variable changed and bind to ControlProperty,ControlProperty seted not because of [.allEditingEvents, .valueChanged]
, then done.
In the source code of controlProperty
, will establish the UIControl target - action.
[.allEditingEvents, .valueChanged]
contains of editingDidBegin, editingChanged, editingDidEnd, editingDidEndOnExit, valueChanged,
So assigning to textField.text
directly will trigger no event.