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が 2 回呼び出されます。nib がロードされているとき、すべてのオブジェクトは で初期化され-initWithCoder:、その後receive になります-awakeFromNib-initWithCoder:次のように、 or-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];
    ...
}
于 2012-07-30T19:55:33.747 に答える