Fixes problem with UITextView two way binding.

This commit is contained in:
Krunoslav Zaher 2016-03-12 21:30:48 +01:00
parent c6a1f799b0
commit 19900bfaac
3 changed files with 38 additions and 6 deletions

View File

@ -36,6 +36,7 @@ extension UITextView {
let textChanged = self?.textStorage
.rx_didProcessEditingRangeChangeInLength
.observeOn(MainScheduler.asyncInstance)
.map { _ in
return self?.textStorage.string ?? ""
}

View File

@ -70,7 +70,11 @@ class APIWrappersViewController: ViewController {
// MARK: UISegmentedControl
segmentedControl.rx_value
// also test two way binding
let segmentedValue = Variable(0)
segmentedControl.rx_value <-> segmentedValue
segmentedValue.asObservable()
.subscribeNext { [weak self] x in
self?.debug("UISegmentedControl value \(x)")
}
@ -79,7 +83,11 @@ class APIWrappersViewController: ViewController {
// MARK: UISwitch
switcher.rx_value
// also test two way binding
let switchValue = Variable(false)
switcher.rx_value <-> switchValue
switchValue.asObservable()
.subscribeNext { [weak self] x in
self?.debug("UISwitch value \(x)")
}
@ -103,7 +111,11 @@ class APIWrappersViewController: ViewController {
// MARK: UISlider
slider.rx_value
// also test two way binding
let sliderValue = Variable<Float>(0.0)
slider.rx_value <-> sliderValue
sliderValue.asObservable()
.subscribeNext { [weak self] x in
self?.debug("UISlider value \(x)")
}
@ -112,7 +124,12 @@ class APIWrappersViewController: ViewController {
// MARK: UIDatePicker
datePicker.rx_date
// also test two way binding
let dateValue = Variable(NSDate())
datePicker.rx_date <-> dateValue
dateValue.asObservable()
.subscribeNext { [weak self] x in
self?.debug("UIDatePicker date \(x)")
}
@ -121,7 +138,11 @@ class APIWrappersViewController: ViewController {
// MARK: UITextField
textField.rx_text
// also test two way binding
let textValue = Variable("")
textField.rx_text <-> textValue
textValue.asObservable()
.subscribeNext { [weak self] x in
self?.debug("UITextField text \(x)")
}
@ -139,7 +160,11 @@ class APIWrappersViewController: ViewController {
// MARK: UITextView
textView.rx_text
// also test two way binding
let textViewValue = Variable("")
textView.rx_text <-> textViewValue
textViewValue.asObservable()
.subscribeNext { [weak self] x in
self?.debug("UITextView event \(x)")
}

View File

@ -34,6 +34,12 @@ public final class MainScheduler : SerialDispatchQueueScheduler {
*/
public static let instance = MainScheduler()
/**
Singleton instance of `MainScheduler` that always schedules work asynchronously
and doesn't perform optimizations for calls scheduled from main thread.
*/
public static let asyncInstance = SerialDispatchQueueScheduler(serialQueue: dispatch_get_main_queue())
/**
In case this method is called on a background thread it will throw an exception.
*/