Binding an NSTableView Column to NSUserDefaults
I did this a year ago, and didn't need to do it again until now. For some reason even though I had the example of my previous project, it still took me several hours to figure out how to do it again. There don't seem to be any straight forward explanations of how to do this (presumably) extremely common task so here's what I did:
- Drag an NSTableView onto an NSWindow.
- Go to the bindings tab in Inspector for the NSTableView.
- Under the "Content" heading, check the "Bind to" box. The following drop down should already be set to "Shared User Defaults Controller."
- Enter the key name of the user defaults setting you wish to map the table column to. For example, "people."
- Go to the bindings for the column.
- Under the value heading, check the 'Bind to' box. Again, the following drop down should already be set to "Shared User Defaults Controller."
- Enter the same key name you entered for the NSTableView ("people").
Now to test this out, you can register some defaults during your app's initialization. Here's some example code:
<style type="text/css">
.code { border: 1px solid #ccc; list-style-type: decimal-leading-zero; padding: 5px; margin: 0; }
.code code { display: block; padding: 3px; margin-bottom: 0; }
.code li { background: #ddd; border: 1px solid #ccc; margin: 0 0 2px 2.2em; }
</style>
.code { border: 1px solid #ccc; list-style-type: decimal-leading-zero; padding: 5px; margin: 0; }
.code code { display: block; padding: 3px; margin-bottom: 0; }
.code li { background: #ddd; border: 1px solid #ccc; margin: 0 0 2px 2.2em; }
</style>
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];If you run into any issues with this example, please let me know and I will revise it to make sure it is more clear.
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:[NSArray arrayWithObjects:@"alice", @"bob", @"jimmy", nil] forKey:@"people"];
[defaults registerDefaults:dictionary];