3
我有一個NSControl子視圖,並且我想在控件不在keyWindow上時更改繪圖。問題是我沒有看到任何反映該州的財產(嘗試enabled
財產,但那不是它)。當窗口是關鍵或沒有繪製NSControl
簡而言之,我可以區分這兩種狀態嗎?
我有一個NSControl子視圖,並且我想在控件不在keyWindow上時更改繪圖。問題是我沒有看到任何反映該州的財產(嘗試enabled
財產,但那不是它)。當窗口是關鍵或沒有繪製NSControl
簡而言之,我可以區分這兩種狀態嗎?
您可以使用NSWindow的keyWindow
屬性,如果你想檢查一下,如果你的控制是第一個響應鍵盤事件也考驗[[self window] firstResponder] == self
。我不相信keyWindow
支持KVO,但有一個NSWindowDidBecomeKeyNotification
和NSWindowDidResignKeyNotification
你可以聽。例如,
- (id)initWithFrame:(NSRect)frameRect;
{
if (self = [super initWithFrame:frameRect])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(display) name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(display) name:NSWindowDidBecomeKeyNotification object:nil];
}
return self;
}
- (void)drawRect:(NSRect)aRect;
{
if ([[self window] isKeyWindow])
{
// one way...
}
else
{
// another way!
}
}
- (void)dealloc;
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
[super dealloc];
}
謝謝!那就是訣竅。我決定添加開始在'viewDidMoveToWindow'中查找通知,並在'viewWillMoveToWindow:'中將其刪除,以避免通知應用程序中的每個窗口。 (需要爲這個簡單的任務註冊一個通知似乎很奇怪......一個很棒的框架中的一個小洞。) – gcamp 2011-05-25 19:37:59