Search code examples
objective-ccocoamacoscocoa-bindings

Bind NSTextField's text color to NSColorWell?


I'm quite new to Cocoa Bindings, but I've seen enough that I'd love to change all my old clunky methods over to it. For example, I have a NSColorWell that changes the text color of some NSTextFields in my view. Seems easy in practice, but it's not working.

Here's how my bindings look for my NSColorWell:

enter image description here

And here's my bindings for my NSTextField:

enter image description here

But instead of displaying a color it just displays NSCalibratedRGBColor.... Obviously it's not setting the value of the color to the field, it's just displaying the raw data.

So, after poking around I tried to make my own NSValueTransformer by doing this:

@interface DataToColor: NSValueTransformer {}
@end
#import "QWDataToColor.h"
@implementation DataToColor
+(Class)transformedValueClass { return [NSColor class]; }
+(BOOL)allowsReverseTransformation { return NO; }
-(id)transformedValue:(id)item {
    NSColor *theColor=nil;
    NSData *theData=[NSData dataWithData:item];
    if (theData != nil)
        theColor=(NSColor *)[NSUnarchiver unarchiveObjectWithData:theData];
    return theColor;
}
@end

Then I set that value transformer to my "Value Transformer" area in my bindings in IB.

However, it still gave the same results. Any ideas?


Solution

  • The value binding is:

    An NSString or NSNumber that is displayed as the content of the NSTextField
    

    You want to bind the textColor property of your NSTextField, not value.

    See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTextField.html for the complete list of bindings supported by NSTextField.