无法通过Interface Builder存储NSTextField的背景颜色

时间:2012-07-30 13:26:07

标签: cocoa colors interface-builder background-color nstextfield

我正在尝试通过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上找到。

1 个答案:

答案 0 :(得分:0)

您的-customize方法被调用两次。当nib加载时,所有对象都使用-initWithCoder:进行初始化,然后接收-awakeFromNib。您应该删除-initWithCoder:-awakeFromNibm_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];
    ...
}