TwoWayBondage
Easy to use two way binding Library. Simpliest way to bind values from ViewModels to Views. Based on SimpleTwoWayBinding
Table of contents
Requirements
- iOS 12.0+
- Swift 5
Installation
TwoWayBondage is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'TwoWayBondage'
To get the full benefits import TwoWayBondage
at the start of the source file:
import TwoWayBondage
Usage Example
Create ViewModel
All of the bindable properties should be declared as Observable in ViewModel.
import TwoWayBondage
struct ExampleViewModel {
let name: Observable<String> = Observable<String>()
let isBroccoliLover: Observable<Bool> = Observable<Bool>()
let isIceCreamLover: Observable<Bool> = Observable<Bool>(true)
}
Create ViewController
To bind an Observable with UIControls and vise versa - bind or bindAndFire should be called in ViewController.
@IBOutlet private weak var nameTextField: UITextField!
@IBOutlet private weak var welcomeLabel: UILabel!
@IBOutlet private weak var iceCreamSwitch: UISwitch!
override viewDidLoad() {
...
bindEvents()
}
private func bindEvents() {
nameTextField.bind(viewModel.name)
viewModel.name.bind { [weak self] name in
self?.welcomeLabel.text = name
}
vieModel.isIceCreamLover.bindAndFire { [weak self] value in
self?.iceCreamSwitch.setOn(value, animated: true)
}
}
License
TwoWayBondage is available under the MIT license. See the LICENSE file for more info.