Binding the background color of an IKImageBrowserView

A little post-it, because I spent a lot of time figuring this out (I’m new to Cocoa, it doesn’t help :p) I have a preference panel, and I’m using the shared user defaults controller to store my application preferences. In this application, I have an IKImageView and an IKImageBrowserView (I’m trying to create a small image viewer application)

Via the interface builder tool, you can easily bind the background color of the IKImageView (it appears on the bindings tab) but for a reason, Apple decided that the background color of the IKImageBrowserView shouldn’t appear on this tab … I think it’s stupid but anyway. Here is the way to programmatically bind the background color of the IKImageBrowserView to a value stored in the user defaults :

// create the options : continuous update and keyed unarchive
// (the color must be archived and unarchived because it's an
// object and can't be store as an int or stuff like that)
NSMutableDictionary * options = [[NSMutableDictionary alloc] init];
[options setObject:[NSNumber numberWithBool:YES]
         forKey:NSContinuouslyUpdatesValueBindingOption];
[options setObject:NSKeyedUnarchiveFromDataTransformerName
         forKey:NSValueTransformerNameBindingOption];

// mImageBrowser is the IKImageBrowserView, and backgroundColor
// is the key used in the shared user defaults controller to store the color
[mImageBrowser bind:IKImageBrowserBackgroundColorKey
               toObject:[NSUserDefaultsController sharedUserDefaultsController]
               withKeyPath:@"values.backgroundColor"
               options:options];
[options release];

It’s quite simple, but it took some time for me to figure out how to retrieve the shared user defaults key and bind it to the background of my IKImageBrowserView

Leave a Reply

Your email address will not be published. Required fields are marked *