4
votes

When a user is mid-editing a textfield on a tableview and decides to click the close button for the window, the changes he/she made does not get saved. How can I force a tableview (view based) to end editing (not abort editing) ?

1
I couldn't make the answer work for me when I was using storyboards and segues to modal sheets in my project. I ended up having my viewController as a delegate of the NSTextField's within the table cell view. Then I toggled the isEnabled flag of the sheet's 'OK' button as the cell view's text field fired control(_ control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool and control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool. It's not pretty, but it forces the user to end editing the cell's text and then confirm their entry. - Todd

1 Answers

2
votes

The simplest way to do this is to assign a delegate for your window and respond to NSWindow's -windowShouldClose: delegate method. In it, invoke -makeFirstResponder: to make the window itself the first responder, noting the BOOL answer it gives. You should return whatever -makeFirstResponder: answers as the answer to the delegate ...ShouldClose: message since it may not be able to end editing / resign first responder. Assuming it's successful, it'll end editing and trigger whatever action / bindings machinery you set up prior to actually closing. This works because NSTableView and the views it uses in view-based mode are subclasses of NSControl and automatically handles the responder status changes by ending editing, etc. Hope this helps.