将 NSTextField 的文本颜色绑定到 NSColorWell 吗?
我对 Cocoa Bindings 还很陌生,但我已经看到了足够多的东西,我很想把我所有旧的笨重方法改成它。例如,我有一个 NSColorWell
可以更改视图中某些 NSTextField
的文本颜色。实践起来看似简单,但行不通。
以下是我的 NSColorWell
的绑定:
这是我的 NSColorWell
的绑定NSTextField
:
但它不显示颜色,而是显示 NSCalibrateRGBColor ...
。显然它没有设置字段的颜色值,它只是显示原始数据。
因此,在摸索之后,我尝试通过执行以下操作来制作自己的 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 中绑定中的“值转换器”区域。
然而,它仍然给出了相同的结果。有什么想法吗?
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 NSTextField
s in my view. Seems easy in practice, but it's not working.
Here's how my bindings look for my NSColorWell
:
And here's my bindings for my NSTextField
:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
值绑定是:
您想要绑定 NSTextField 的 textColor 属性,而不是值。
请参阅 http://developer.apple.com /library/mac/#documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTextField.html 了解 NSTextField 支持的绑定的完整列表。
The value binding is:
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.
这很简单。您可以通过 IB 同等地绑定代码中的内容,在这种情况下,我认为这是唯一的方法,因为视图(在这种情况下,颜色井)不会在 IB* 中显示为可绑定对象:
也可以,并且在某些情况下需要,例如当您可能需要在其他地方使用该值或将其存档时,通过控制器中的中间变量绑定每个对象。
*我很乐意在这方面被证明是错误的!
This is pretty simple. You can bind things in code equally as well as through IB, and in this case I think this is the only way to do it, because views (in this case, the color well) don't show up as bindable objects in IB*:
It's also possible, and in some cases desirable, such as when you might need to use this value in other places or archive it, to bind each object through an intermediate variable in a controller.
*I would love to be shown wrong about this!