Re: Easy question about bindings for a custom view
Re: Easy question about bindings for a custom view
- Subject: Re: Easy question about bindings for a custom view
- From: Kyle Sluder <email@hidden>
- Date: Thu, 19 Mar 2009 14:39:17 -0400
2009/3/19 Ulai Beekam <email@hidden>:
> I want to create a custom view that displays (editable) text fields on the top of each other, as many as it takes, that display the *first names* of the currently selected items in employeesArray. So naturally, how many text fields are displayed depend on how many items in employeesArray are selected.
Are you sure you don't just want to use an NSTableView, maybe with a
custom data cell?
> My question is: How would I implement the 'value' binding for my custom view? Can you point me in the right direction? Should I use an NSMutableArray that updates on the fly as selection changes in the array controller? Or what? I'm a bit stuck.
Follow the MVC pattern as exemplified by NSTableColumn: don't store
anything. Instead, use an NSTextFieldCell to draw the contents of
your view and handle editing.
A note about implementing binding to collections (to-many relations):
make sure in your implementation of
-bind:toObject:withKeyPath:options: you register for KVO notifications
from every object in the collection. This means you will have to
un/rebind in -observeValueForKeyPath:ofObject:change:context:. Due to
a "why the hell have they not fixed this yet"-level bug in the
NSController classes[1], the change dictionary doesn't contain the
old/new values keys, which means you will need to iterate through the
new objects and compare them to the objects you're already observing,
removing yourself as an observer of the ones that are no longer there
and adding yourself as an observer of the new ones.
[1] http://homepage.mac.com/mmalc/CocoaExamples/controllers.html#ibBindings
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden