- control:textShouldBeginEditing:
- control:textShouldEndEditing:
These two protocol methods are invoked on the delegate object. Everyday's Interface Builder outlet connection stuff, I thought, unaware I'd lose the rest of the day trying to have those method called. I then selected the text field in the editable column whose editing I wanted to be notified of:
Then I selected the Connection Inspector and connected the delegate outlet to the target object:
Finally, I implemented the two aforementioned methods on the delegate object, ran the application and... nothing happened!
The Official Documentation
I'll won't tell about the frustration I felt trying to do one of the simplest and most paradigmatic tasks in Cocoa programming: setting a delegate and implementing the corresponding protocol. Suffice it to say, I felt like I had some evident bug in front my eyes but could not notice it. Not immediately, not after a cup of tea or two, not after a hour diving into the documentation I could find.I revised the documentation, outlets and the code over and over again, but I could not detect the problem. Then I noticed that I was not the only one struggling with this issue, although no solution was provided.
To make a long story short, the documentation I checked seemed confirmed the it should have worked, until I found especially after reading an official Apple Technical Q&A, number QA1551, titled Detecting the start and end edit sessions of a cell in NSTableView, stating the following:
A: How do I detect start and end edit sessions of a cell in NSTableView?
In order to detect when a user is about to start and end an edit session of a cell in NSTableView, you need to be set as the delegate of that table and implement the following NSControl delegate methods:- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor;
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor;The table forwards the delegate message it is getting from the text view on to your delegate object using the control:textShouldEndEditing: method. This way your delegate can be informed of which control the text view field editor is acting on its behalf.
So, you should not set the editable field delegate but the table one! Fortunately, that was easy to change. I removed the connection to the delegate outlet in the text field and set the table one instead. I started the application and... nothing happened again!
At this point I felt at a loss and started to wonder whether some code in my application was interfering with the editing notification path. I created a new Cocoa application and quickly replicated the solution suggested in the Apple documentation but the result was the same: the delegate methods were not being called.
The Solution
The solution was simple, and fortunately not very different from what a Cocoa programmer expects: setting a delegate and implementing a protocol. But while the protocol was the correct one (NSControlTextEditingDelegate), the delegate was not. Better yet: they were not.In fact, after some of tests I reached the following conclusions:
- A cell-based NSTableView instance works as expected: setting the table delegate causes the NSControlTextEditingDelegate protocol methods to be invoked on it.
- A view-based NSTableView instance (such as mine and almost surely most of the new NSTableView instances out there) required both the table and the field delegates to be set for the NSControlTextEditingDelegate protocol methods to be invoked.
I suspected that the aforementioned Apple Technical Q&A QA1551 document only considers the case of cell-based tables despite its date of publication: view-based tables were introduced in OS X 10.6, shipped two months before this document was issued. To dispel my doubts I decided to experiment with both kinds of table and found the behaviour described above, one for each different kind of table.
Nevertheless, and even though I dedicated a lot of time to skimming through the documentation, I was unable to find a mention of this issue in any other guide, API documentation or technical article from Apple. Even the very good Table View Programming Guide for Mac makes no mention of this "peculiarity" despite being a fairly common use case.
I hope this post helps anybody falling into the same trap to quickly solve his issues with getting NSControlTextEditingDelegate notifications to his table delegate.
Thanks! Had a similar problem with delegate methods "controlTextDidBeginEditing" and "controlTextDidEndEditing". I also spent hours researching the issue until I found your blog. Thanks, again.
ReplyDeleteThanks! The same problem was beginning to annoy me!
ReplyDeleteDitto! Thanks for working this out and publishing this.
ReplyDeleteI spent all afternoon reading the documentation and trying to get this to work. I'm a newbie but still, the Apple documentation is either too dated or just unclear. I read your post and, in five minutes, everything worked. You rock!!!
ReplyDeleteI'm glad it helped!
ReplyDeleteThank you very much. Had to search long and hard to find this post. Definitely improved my understanding of Table Views.
ReplyDeleteSo I'm indeed using a view-based NSTableView. I did assign delegate for both my TableView and my Table View Cell to be the ViewController. I did it using NIB editor. However I'm not getting the expected result.
While my tableView methods are being called in my NSTableViewDelegate ViewController Extension, my control methods are not being called. Appreciate if you can suggest what I might be doing wrong.
extension ViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
// DOES get called and allows me to populate my table
cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
// ... code to populate omitted
return cell
}
func control(_ control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool {
// Does not get called when I start editing
return true
}
func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
// Does not get called when I end editing
return true
}
}
Do I need to include the control methods in separate extension that implements the NSControlTextEditingDelegate protocol? Since NSTableViewDelegate implements NSControlTextEditingDelegate I thought I shouldn't have too