23

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?

fspirit
  • 2,537
  • 2
  • 19
  • 27

8 Answers8

15

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

Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
Jake
  • 624
  • 5
  • 6
  • 2
    I couldn't get the above link to work but the link below did work for me. http://developer.apple.com/library/iOS/#documentation/General/Conceptual/Devpedia-CocoaApp/KVO.html – Robotic Cat Jan 29 '11 at 16:30
  • 6
    [Key-Value Programming Guide - Google Search](http://www.google.com/search?client=safari&rls=en&q=Key-Value+Coding+Programming+Guide&ie=UTF-8&oe=UTF-8&redir_esc=&ei=M-TWT_33O66hmQW__aTxAg) Hopefully this is a more reliable link :) – nacho4d Jun 12 '12 at 06:40
  • 2
    I guess you meant: [Key-Value Observing Programming Guide](http://www.google.com/search?q=Key-Value+Observing+Programming+Guide) – jdtogni Aug 01 '13 at 22:19
  • Yes, it is a brilliant idea – sahara108 Mar 17 '16 at 03:10
15

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:

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
12

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);
}];
Roman B.
  • 3,598
  • 1
  • 25
  • 21
3

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)
  }
Srđan Rašić
  • 1,597
  • 15
  • 23
3

Don't forget NSFetchedResultsController.

Not a full blown data bound controller, but makes table views a lot easier to use with Core Data.

Abizern
  • 146,289
  • 39
  • 203
  • 257
2

STV (http://sensiblecocoa.com) is a framework that can do that within tableviews

brainray
  • 12,512
  • 11
  • 67
  • 116
1

I use CoreDataTableViewController from the Stanford University for my TableViewControllers. It hides a lot of details that you would normally implement in your TableViewController.

Googling for CoreDataTableViewController.h and .m will help you on the road. There are versions from several courses available, the latest does ARC, the old ones don't.

For syncing labels and edit fields with an NSManagedObject, I am still looking for a good solution.

Bjinse
  • 1,339
  • 12
  • 25
0

Yes, there is a data binding framework that integrates well into Interface Builder and requires only minimal code overhead (if at all).

Take a look at https://github.com/mutech/aka-ios-beacon

EDIT: You can for example bind a table view to a fetched results controller simply by setting the data source binding property of the table view in interface builder to:

[ yourResultsController ] { defaultCellMapping: "YourCellId" }

And the only thing you have to do is to define a property yourResultsController in your view controller.

The wiki provides a rather complete documentation and a lot of example use cases.

Michael
  • 250
  • 3
  • 8