3

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!

nomothetis
  • 233
  • 2
  • 7

1 Answers1

-1

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.)

Richard
  • 3,316
  • 30
  • 41
  • Thanks, I'll try that! I'll be back to mark your answer as correct as soon as I get it working. :-) – nomothetis May 12 '11 at 04:12
  • Well, it's consistent with the behavior that I'm seeing: if I click away from it, the proper background appears. I don't have time to work on it today, but I will soon, so I'll confirm. – nomothetis May 12 '11 at 15:11
  • I wasn't able to get it working. Very sad. It seems to have to do with the selection state of the field. Who knows, though? – nomothetis May 17 '11 at 04:20