我正在尝试通过Interface Builder在成员变量中缓存NSTextField
的背景颜色集,以便以后在另一个组件中使用。启动时,NSTextField
的背景颜色设置为透明。
@implementation CTTextField
- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
if (self) {
[self customize];
}
return self;
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self customize];
}
return self;
}
- (void)awakeFromNib {
...
[self customize];
}
- (void)customize {
// Store the user defined background color.
// FIXME: The color is not stored.
m_userDefinedBackgroundColor = self.backgroundColor;
// Disable the background color.
self.backgroundColor = [NSColor colorWithCalibratedWhite:1.0f alpha:0.0f];
...
}
@end
但是,m_userDefinedBackgroundColor
始终为黑色
我正在处理的整个CocoaThemes project可以在GitHub上找到。
答案 0 :(得分:0)
您的-customize
方法被调用两次。当nib加载时,所有对象都使用-initWithCoder:
进行初始化,然后接收-awakeFromNib
。您应该删除-initWithCoder:
或-awakeFromNib
或m_userDefinedBackgroundColor
nil
-customize
,如下所示:
- (void)customize {
// Store the user defined background color.
// FIXME: The color is not stored.
if (m_userDefinedBackgroundColor == nil)
m_userDefinedBackgroundColor = self.backgroundColor;
// Disable the background color.
self.backgroundColor = [NSColor colorWithCalibratedWhite:1.0f alpha:0.0f];
...
}