问题描述
|
我有一个NSControl子视图,当控件不在keyWindow上时,我想更改图形。问题是我没有看到任何反映该状态的属性(尝试了“ 0”属性,但事实并非如此)。
简单来说,我可以区分这两种状态吗?
解决方法
您可以使用NSWindow的
keyWindow
属性,如果要检查控件是否是键盘事件的第一响应者,还可以测试[[self window] firstResponder] == self
。我不相信ѭ1支持KVO,但是您可以收听ѭ4和ѭ5。例如,
- (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];
}