0

I subclassed NSSecureTextField, and overrided -(BOOL)becomeFirstResponder,but it only works well when my custom NSSecureTextField get focus at the first time.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Gaojin
  • 1
  • 2

1 Answers1

0

I think it is not a problem of NSSecureTextField. It is a problem of how a control get focused.

Create a new project and only drag two NSSecureTextField on your view. Set the custom class of one of them to MySecureTextField defined below and keep the other one default. Run the project and change focus between the two NSSecureTextField, you will see the "Get focus" printed when the custom one get focus each time.

Back to your program, please check if the NSSecureTextField is lost focus? Does the resignFirstResponser called?

#import "MySecureTextField.h"

@implementation MySecureTextField

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

- (BOOL)becomeFirstResponder
{
    NSLog(@"Get focus");
    return [super becomeFirstResponder];
}
@end
flashlib
  • 151
  • 1
  • 6
  • that's pretty weird! in my demo program. if you override `-(void)textDidEndEditing:` and `-(BOOL)becomeFirstResponder` both, then `-(BOOL)becomeFirstResponder` would be called only when MySecureTextField gets focus at the first time and never be called again. – Gaojin Sep 26 '13 at 02:45
  • @Gaojin: What about flashlib's question? If you override `resignFirstResponder`, does that get called? – Peter Hosey Sep 30 '13 at 02:01
  • @PeterHosey `-(BOOL)becomeFirstResponder` and `-(BOOL)resignFirstResponder`would be called at the same time when the subclassed `NSSecureTextField` gets focus each time.However,if I also override `-(void)textDidEndEditing`,the weird situation comes again. – Gaojin Oct 08 '13 at 06:17
  • @Gaojin: I wonder if the field editor is relevant. It's a text view (strictly speaking, some kind of NSText, not necessarily an NSTextView) that takes focus whenever a field or field cell receives focus. Might not be relevant, since this is a secure text field and that's an entirely separate possible reason, but it's something to look into. – Peter Hosey Oct 08 '13 at 06:41