Kiran P Nair
Kiran P Nair

Reputation: 2021

how to achieve two way binding using ReactiveCocoa 4.0 and MVVM

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")

Upvotes: 1

Views: 1208

Answers (2)

linimin
linimin

Reputation: 6377

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 }

Upvotes: 4

DAN
DAN

Reputation: 939

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:

Upvotes: 0

Related Questions