0

我正在尝试NSTextField通过 Interface Builder 将背景颜色集缓存在成员变量中,以便以后在另一个组件中使用。启动时,背景颜色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 项目都可以在 GitHub 上找到。

4

1 回答 1

0

你的-customize方法被调用了两次。当 nib 正在加载时,所有对象都在初始化,-initWithCoder:然后接收-awakeFromNib. 您应该像这样删除您的或-initWithCoder:-awakeFromNib检查:m_userDefinedBackgroundColornil-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];
    ...
}
于 2012-07-30T19:55:33.747 回答