ControlTextDidEndEditing (how to determine which textfield called)

Hi, I have a number of NSTextFields on a ViewController and I'm searching for a way to determine which textfield called the DidEndEditing method. Is there a way to do it without adding an IBOutlet for each TextField?

func controlTextDidEndEditing(_ obj: Notification) {      }

Also, the IBOutlet question raises another question. Is there an equivalent of UIKits IBOutletCollection for AppKit? I've been googling away but haven't found much about it so far.

Cheers for any help.

Answered by Claude31 in 730931022

a way to determine which textfield called the DidEndEditing method. Is there a way to do it without adding an IBOutlet for each TextField?

You could use tag:

  • give a tag to each TextField
  • You can also create constant to ease reading
let tagForTextField1 = 1
let tagForTextField2 = 2
  • Retrieve it in the Notification
    func controlTextDidEndEditing(_ obj: Notification) {
        
        guard let sender = obj.object as? NSTextField else {
            return
        }
       let tag = sender.tag

       switch tag {
           case tagForTextField1 : // do what's appropriate
           case tagForTextField2 : // do what's appropriate
           default: return
       }
    }

.

Is there an equivalent of UIKits IBOutletCollection for AppKit?

No there is not, unfortunately and bizarrely. You could file a bug report for improvement…

Accepted Answer

a way to determine which textfield called the DidEndEditing method. Is there a way to do it without adding an IBOutlet for each TextField?

You could use tag:

  • give a tag to each TextField
  • You can also create constant to ease reading
let tagForTextField1 = 1
let tagForTextField2 = 2
  • Retrieve it in the Notification
    func controlTextDidEndEditing(_ obj: Notification) {
        
        guard let sender = obj.object as? NSTextField else {
            return
        }
       let tag = sender.tag

       switch tag {
           case tagForTextField1 : // do what's appropriate
           case tagForTextField2 : // do what's appropriate
           default: return
       }
    }

.

Is there an equivalent of UIKits IBOutletCollection for AppKit?

No there is not, unfortunately and bizarrely. You could file a bug report for improvement…

ControlTextDidEndEditing (how to determine which textfield called)
 
 
Q