Is there any data binding mechanism available for iOS?
Solution 1
The closest thing in Cocoa is 'Key-Value Observing'. In the desktop Cocoa framework you can use bindings to hook user interface elements up to underlying objects so that changes in the objects or UI elements are reflected in the other.
Whilst Cocoa on iOS doesn't have this sort of UI bindings, you can still use 'Key-Value Observing' to synchronise changes in the data model with UI elements as described here:
http://developer.apple.com/library/iOS/#documentation/General/Conceptual/Devpedia-CocoaApp/KVO.html
Solution 2
I wrote a little open-source library that provides some simple data-binding functionality. It's basically just a wrapper around key-value observing (KVO).
There are a few other similar libraries on GitHub:
- http://github.com/dewind/KeyPathBindings
- http://github.com/jonsterling/Observe
- http://github.com/mruegenberg/objc-simple-bindings
- http://github.com/zeasy/EasyBinding
Solution 3
Probably should also mention Github's Reactive Cocoa, a framework for composing and transforming sequences of values, an objective-C version of .NET's Reactive Extensions (Rx).
Binding mechanics can be done really simple (from the sample):
// RACObserve(self, username) creates a new RACSignal that sends a new value
// whenever the username changes. -subscribeNext: will execute the block
// whenever the signal sends a value.
[RACObserve(self, username) subscribeNext:^(NSString *newName) {
NSLog(@"%@", newName);
}];
Solution 4
Don't forget NSFetchedResultsController
.
Not a full blown data bound controller, but makes table views a lot easier to use with Core Data.
Solution 5
If you're using Swift, check out Bond framework: https://github.com/ReactiveKit/Bond
Binding is as simple as:
textField.reactive.text.bind(to: label.reactive.text)
It plays well with functional:
textField.reactive.text
.map { "Hi " + $0 }
.bind(to: label.reactive.text)
And provides simple observations:
textField.reactive.text
.observeNext { text in
print(text)
}
Related videos on Youtube
fspirit
Updated on February 09, 2020Comments
-
fspirit about 4 years
In .NET I just do something like DataForm.Source = Object and then magic happens. Platform routes data changes from ui fileds to object properties, does validation and so on. Can I do something similar with Cocoa Touch and CoreData objects?
-
Robotic Cat about 13 yearsI couldn't get the above link to work but the link below did work for me. developer.apple.com/library/iOS/#documentation/General/…
-
nacho4d almost 12 yearsKey-Value Programming Guide - Google Search Hopefully this is a more reliable link :)
-
jdtogni over 10 yearsI guess you meant: Key-Value Observing Programming Guide
-
sahara108 about 8 yearsYes, it is a brilliant idea