为什么 NSTextField 不重绘焦点字段的背景?

发布于 2024-11-07 02:32:54 字数 1583 浏览 4 评论 0原文

我遇到了一个奇怪的问题。我有两个 NSTExtFields 代表用户名和密码。当我得到不正确的用户名/密码组合时,我设置一个实例变量,在包含这两个字段的视图的控制器中进行说明。然后,当实例变量更改时,我使用键值观察来运行以下方法:

- (void)handleCredentialsValidityToggle {
    if ([self areCredentialsValid]) {
        [passwordField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
        [usernameField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
    } else {
        [passwordField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0
                                                                    green:1.0
                                                                     blue:0.35
                                                                    alpha:1.0]];
        [usernameField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0
                                                                    green:1.0
                                                                     blue:0.35
                                                                    alpha:1.0]];
    }
}

只要字段不活动,该方法就可以正常工作。换句话说,如果我修改其中一个字段,将光标保持在焦点位置,并尝试进行身份验证(通过单击菜单项),则具有焦点的字段不会更新为新的背景颜色,而没有焦点的字段确实如此。

为了看看我是否可以强制它更新,我在方法的末尾添加了以下几行:

[passwordField drawCell:[passwordField cell]];
[usernameField drawCell:[usernameField cell]];

仍然没有运气。任何人都知道可能导致此问题的原因是什么?提前致谢!

I'm running into a weird problem. I have two NSTExtFields representing a username and a password. When I get an incorrect username/password combination, I set an instance variable stating so in the controller of the view containing those two fields. I then use key-value observing to run the following method when the instance variable changes:

- (void)handleCredentialsValidityToggle {
    if ([self areCredentialsValid]) {
        [passwordField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
        [usernameField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
    } else {
        [passwordField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0
                                                                    green:1.0
                                                                     blue:0.35
                                                                    alpha:1.0]];
        [usernameField setBackgroundColor:[NSColor colorWithCalibratedRed:1.0
                                                                    green:1.0
                                                                     blue:0.35
                                                                    alpha:1.0]];
    }
}

This works fine as long as a field isn't active. In other words, if I modify the one of the fields, keep it in focus with the cursor inside, and try to authenticate (by clicking a menu item), the field with the focus does not update to the new background color, while the field without the focus does.

In order to see if I could force it to update, I added the following lines to the end of the method:

[passwordField drawCell:[passwordField cell]];
[usernameField drawCell:[usernameField cell]];

Still no luck. Anyone have any ideas what could be causing this? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-11-14 02:32:54

我的猜测是它正在您的自定义背景颜色之上绘制编辑框。您可以尝试子类化 NSTextFieldCell 并覆盖 editWithFrame:inView:editor:delegate:event:
(当然,请确保您的单元格为 setDrawsBackground:YES。)

My guess is that it is drawing the editing frame on top of your custom background color. You can try subclassing NSTextFieldCell and overriding editWithFrame:inView:editor:delegate:event:.
(And of course make sure that your cell is setDrawsBackground:YES.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文