为什么 NSTextField 不重绘焦点字段的背景?
我遇到了一个奇怪的问题。我有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是它正在您的自定义背景颜色之上绘制编辑框。您可以尝试子类化
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 overridingeditWithFrame:inView:editor:delegate:event:
.(And of course make sure that your cell is
setDrawsBackground:YES
.)