I have a text field cell in a table view, from which I need to be made aware when it ends editing. I thought I would set my Controller class as the text field cell’s delegate, and then use NSTextField’s delegate method textDidEndEditing:, but realized that the text field cell doesn’t seem to have delegate methods? Why is this, and what can I do (other than subclassing) to be informed when editing is finished?
Thanks
NSTextFieldCellinherits fromNSCell(well, technically fromNSActionCellwhich inherits fromNSCell). TheNSCellclass is used to (from the docs):Notably, The cell class is used for “displaying text or images”, and not dealing with interaction with the user. Similarly, with the
NSTextFieldclass:The
NSTextFielddeals with the actual user input, whilst using the text field cell to simply implement its user interface, and similarly, the delegate methods to provide notification when the editing of text has ended is provided through theNSTextFieldclass and not through theNSTextFieldCellclass.If you want to be notified of when editing ends in an
NSTableView, then you need to register yourself as an observer of theNSTextDidEndEditingNotification(you might want to read the NSNotificationCenter class reference if you are unfamiliar with notifications). To do this, place the following in your controller class; theawakeFromNibfunction is a good place to include it to ensure that it is called upon your application’s startup:Where
tableViewis the pointer to yourNSTableViewobject. Then, simply implement the method as follows:Don’t forget to remove yourself as an observer upon deallocation:
The reason that you set the object that you are observing to be the
NSTableViewinstance (and not the cell itself) is that under the hood, when you edit a cell in the table, the cell that you are dealing with isn’t being edited directly; it is the window’s (or a custom) field editor. When editing ends, the field editor then passes the new value for that cell on to the table view. However the table view will post a notification to say that a cell has finished being edited.