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) ?
4
votes
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.
viewControlleras a delegate of theNSTextField's within the table cell view. Then I toggled theisEnabledflag of the sheet's 'OK' button as the cell view's text field firedcontrol(_ control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Boolandcontrol(_ 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