Reputation: 122391
I have several panels that contain NSTextField
controls bound to properties within the File's Owner object. If the user edits a field and then presses Tab, to move to the next field, it works as expected. However if the user doesn't press Tab and just presses the OK button, the new value is not set in the File's Owner object.
In order to workaround this I have set Updates Continuously in the binding, but this must be expensive (EDIT: or at least it's inelegant).
Is there a way to force the bind update when the OK button is pressed rather than using Updates Continuously?
Upvotes: 2
Views: 1333
Reputation: 7560
Although this is really old, I absolutely disagree with the assumption that this question is based on.
Countinously updating the binding is absolutely not expensive. I guess you might think this updates the value continuously, understanding as "regularly based on some interval".
But this is not true. This just means it updates whenever you change the bound value. This means, when you type something in a textView, it would update as you write; this is what you'd want in this situation.
Upvotes: 0
Reputation: 46020
You're right that you don't need to use the continuously updates value option.
If you're using bindings (which you are), then what you should be doing is calling the -commitEditing
method of the NSController
subclass that's managing the binding. You'd normally do this in your method that closes the sheet that you're displaying.
-commitEditing
tells the controller to finish editing in the active control and commit the current edits to the bound object.
It's a good idea to call this whenever you are performing a persistence operation such as a save.
Upvotes: 6
Reputation: 122391
The solution to this is to 'end editing' in the action method that gets called by the OK button. As the pane is a subclass of NSWindowController
, the NSWindow
is easily accessible, however in your code you might have to get the NSWindow
via a control you have bound to the controller; for example NSWindow *window = [_someControl window]
.
Below is the implementation of my okPressed
action method.
In summary I believe this is a better solution to setting Updated Continuously in the bound controls.
- (IBAction)okPressed:(id)sender
{
NSWindow *window = [self window];
BOOL editingEnded = [window makeFirstResponder:window];
if (!editingEnded)
{
logwrn(@"Unable to end editing");
return;
}
if (_delegateRespondsToEditComplete)
{
[_delegate detailsEditComplete:&_mydetails];
}
}
Upvotes: 3