1

In swift Cocoa is there an event that fires when a NSTextField gets focus for editing. I Come from C# which has GotFocus() and LostFocus() Events.

For NSTextfield I could find only these functions which fire after one starts typing. Not as soon as focus is gained.

 override func controlTextDidBeginEditing(obj: NSNotification) {
 }

 func control(control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool {

 }

I Need this because as soon as someone starts editing, I want to show more detail. I have a simpler display name, and full name for editing.

Thanks,

Gavin
  • 203
  • 2
  • 11

1 Answers1

3

You can subclass NSTextField and override becomeFirstResponder and resignFirstResponder. Please check this link How to detect when NSTextField has the focus or is it's content selected cocoa

Community
  • 1
  • 1
Sheen Vempeny
  • 818
  • 1
  • 5
  • 8
  • Please Check Ur Spelling NSTextfield "UITextfield" – Sumit singh Mar 02 '15 at 11:11
  • 2
    @Abhay, NSTextField is in Cocoa and UITextField in UIKit. It is not a spelling mistake – Sheen Vempeny Mar 02 '15 at 11:18
  • 3
    If you're going to use ``becomeFirstResponder`` and ``resignFirstResponder`` it'll also be worth your while to become familiar with the operation of the window's ``fieldEditor``. This object (an ``NSTextView`` subclass) is where you're editing *actually* happens and it's worth knowing about because when you think your ``NSTextField`` has focus, it doesn't - the field editor does. Appreciating this help you understand why you're ``NSTextField`` is *not* the first responder even when you're happily typing text into it. – Paul Patterson Mar 02 '15 at 13:47
  • Excellent!. I had to call a custom delegate from inside overriding the become first responder then listen for that "event". – Gavin Mar 03 '15 at 09:01