Exposing Cocoa bindings on a custom control

For quite a while now, I have been working on-and-off (more off than on) at learning Objective-C and Cocoa with an eye to making a couple of things for personal use.

Cocoa's bindings are totally amazing. While making a custom control I suddenly thought I wanted to make it able to use the bindings system to get/set the control value. Below is the code to do this:

+ (void)initialize {
	[self exposeBinding:@"dateValue"];
}

Great isn't it?

As long as your accessors are already key-value-coding compliant (ie. are of the form value/setValue; firstName/setFirstName etc.), you just call exposeBinding for each binding you want to create and that's it!

Ah, less code, less bugs. Love it.

Permalink

Comments

Add a comment

Your email address will not be shared, sold, or made visible on the site.