我对Cocoa绑定还是个新手,但是我已经看得够多了,所以我想把我所有的笨拙的老方法都改到它上面去。例如,我有一个更改视图中某些NSTextField的文本颜色的NSColorWell。在实践中看起来很简单,但它并不起作用。
下面是我的NSColorWell绑定的外观

下面是我的NSTextField绑定

但它只显示NSCalibratedRGBColor...,而不是显示颜色。显然,它并没有设置字段的颜色值,它只是显示原始数据。
因此,在尝试之后,我尝试通过这样做来制作我自己的NSValueTransformer:
@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然后,我在IB中的绑定中将该值转换器设置为"Value Transformer“区域。
然而,它仍然给出了相同的结果。有什么想法吗?
发布于 2011-05-06 10:12:45
值绑定是:
An NSString or NSNumber that is displayed as the content of the NSTextField您希望绑定NSTextField的textColor属性,而不是值。
有关NSTextField支持的绑定的完整列表,请参阅http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTextField.html。
https://stackoverflow.com/questions/5906023
复制相似问题