I put the text box in the window, and I want the text box to draw the background only when focused. I know that all controls in a window have one field editor. I tried a subclass of nstextfield and implemented startFirstResponder and resignFirstResponder. And tried using the custom Singleton editor for the window.
Does anyone know how to achieve this?
In NSWindow, each text field or button shares one instance of the field editor (one instance of NSTextView), so when you click on the text field, the text field first becomes the first Responser and then quickly passes it to the shared field editor. Therefore, when the text field has lost focus, the resignFirstResponder of the text field will never be called (because the field editor is FirstResponder).
You can look at fieldEditor: forObject: in the NSWindow API.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/fieldEditor:forObject :
SOLUTION:
(Thanks, Michael Gorbach) In my window controller
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
NSText *text = [sender fieldEditor:YES forObject:self];
if(text&&[anObject isKindOfClass:[MyCustomTextField class]])
{
[text setBackgroundColor:[NSColor whiteColor]];
[text setDrawsBackground:YES];
}
return text;
}
source