Is there any data binding mechanism available for iOS?

18,875

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:

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)
  }
Share:
18,875

Related videos on Youtube

fspirit
Author by

fspirit

Updated on February 09, 2020

Comments

Related