1

I am working in a project using Reactive Cocoa 4.0 and MVVM(swift 2.0).In view model I have string that will update according to the textfield changes,but i need to update that textfield if the string changes.Is it possible for two way binding between textfield and string(ageString).

txtAge.rac_textSignal() ~> RAC(objViewModel, "ageString")
Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
Kiran P Nair
  • 2,041
  • 22
  • 31

2 Answers2

4

You can try code below:

viewModel.ageString.producer
         .skipRepeats { [weak self] in
                 $1 == self?.txtAge.text
         }.startWithNext { [weak self] in
                 self?.txtAge.text = $0
}

viewModel.ageString <~ txtAge.rac_textSignal()
        .toSignalProducer()
        .map { ($0 as? String) ?? "" }
        .flatMapError { _ in SignalProducer<String, NoError>.empty }
linimin
  • 6,239
  • 2
  • 26
  • 31
0

Before RAC 2.x you could use RACChannels, which offered this functionality (you can subscribe to each channel terminal and be notified when new values arrive). Channels and terminals haven't been ported to RAC 2.x, so today there is no two-way binding in RAC, but it shouldn't be a big deal as you can always be able to find a different (imho better) approach.

For more details you can find many threads about this topic in the framework's issues on github, such as:

DAN
  • 919
  • 1
  • 6
  • 23