[SOLVED] NSTableView vs. NSUserDefaultsController
[SOLVED] NSTableView vs. NSUserDefaultsController
- Subject: [SOLVED] NSTableView vs. NSUserDefaultsController
- From: Des Courtney <email@hidden>
- Date: Mon, 23 May 2005 10:30:19 -0500
The gist of what I'm trying to do: I'm populating the table view
with a set of rows that contains several user visible columns
and a hidden "column" that acts as a row ID for storage in the
prefs file. I've made an example app of what I've made so far
while trying to figure out a pure-bindings way of doing things...
<http://users.ameritech.net/wormwood/BoundTable.zip>
It took me all weekend and today to figure it out, but I have a
working solution. Though I'm not all that happy with it...
- I removed the two NSObjectControllers, since they were not
helping me any. This left me just the NSArrayController and
my "model" object to populate it.
- After filling the array, I register the model object as a
Key Value Observer on the array object. (Once again, I'm now
mashing up the MVC pattern.) I pass in the array controller
as the "context" for the observation.
- The key value observation code is as follows...
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object
change:(NSDictionary*)change context:(void*)context {
if ( [keyPath isEqualToString:@"selection.colTwo"] ) {
NSString* theNewValue;
NSUserDefaultsController* thePrefs;
theNewValue = [(NSArrayController*)context
valueForKeyPath:@"selection.colTwo"];
thePrefs = [NSUserDefaultsController sharedUserDefaultsController];
[thePrefs setValue:theNewValue forKeyPath:@"values.storedValue"];
} // keyPath
} // observeValueForKeyPath:ofObject:change:context:
...I couldn't get the change dictionary to contain the new value,
despite setting the correct options flags, so I ended up ignoring
it. For the return trip, (that is, reading the prefs file to
set the table view selection), I simply read in the storedValue
at init time and call [NSArrayController setSelectionIndex:] after
scanning the array for the correct index.
My displeasure from the above fix is from the fact that I've now
re-written "glue code," which is what the bindings system is supposed
to eliminate. I'm also using roughly the same amount of code as my
data source/delegate version required. The only advantage of this
approach seems to be that I'm now only using one one "outlet"
connection (to the array controller), with all the other interactions
being explicitly spelled out in the Binding palette of Interface
Builder.
Is this approach the Right Way to solve this problem, or does Cocoa
provide a more elegant solution to the above problem?
Des
--
Des Courtney - Mascon Global, Ltd.
Computer Programmer - Macintosh Specialist
<mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden