resignFirstResponder and becomeFirstResponder

I have subclassed 3 macOS controls: NSCheckbox, NSTextField and NSButton (this one to have a checkbox) and I have override the methods resignFirstResponder and becomeFirstResponder as following:



override open func resignFirstResponder() -> Bool {
        Swift.print("\(identifier!.rawValue) resignFirstResponder")
        return super.resignFirstResponder()
    }

    override open func becomeFirstResponder() -> Bool {
        Swift.print("\(identifier!.rawValue) becomeFirstResponder")
        return super.becomeFirstResponder()
   }

I thought that when I focus a control (with keyboard Tab or with a mouse click) I would have resignFirstResponder called for the control having the focus and becomeFirstResponder called for the control taking the focus.


But it is not exactly like that. I get as result:



identCheck becomeFirstResponder
identCheck resignFirstResponder
identText1 becomeFirstResponder
identText1 resignFirstResponder


I don't understand why the method resignFirstResponder is called for identText1, after it becomed firstResponder.

Can you confirm the user interaction


identCheck becomeFirstResponder

That's when you click the checkbox ?


Then you click in textField identText1

identCheck resignFirstResponder

identText1 becomeFirstResponder


When does this occur ?

identText1 resignFirstResponder

I first make identCheck first responder and then I click on identText1

I understand why I have

identCheck resignFirstResponder  
identText1 becomeFirstResponder

but I don't understand why identText1.resignFirstResponder is then called

My guess would be that "super.becomeFirstResponder()" is returning false.

I change the becomeFirstResponder method in


override open func becomeFirstResponder() -> Bool {
        let bRes =  super.becomeFirstResponder()
        Swift.print("\(identifier!.rawValue) becomeFirstResponder \(bRes)")
        return bRes
}

the super.becomeFirstresponder returns true

And when you type something, does it shows inside identText1 ?

Yes it shows. identText1 is the first responder, even if resignFirstResponder is called. My problem is when a control call resignFirstResponder, I do control validation for the control at this time. that's why I need to know why resignFirstResponder is called

Exact same problem here, did you find out why Patrice ?

When text fields have focus, they aren't actually the first responder. Cocoa creates a special text view, called the field editor, and temporarily adds it to the view hierarchy, covering the text field. It makes that text view the first responder. This text view is what handles all user interaction.

Hi Ken,


yup further googling did make me realize this too.

Since I need to know when the textfield has focus, I've overriden mouseDown: instead of become/resign first responder.

resignFirstResponder and becomeFirstResponder
 
 
Q