窗口是否为关键时绘制NSControl

问题描述

| 我有一个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];
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...