views:

228

answers:

3

In my little cocoa application I have bound the properties of a class to some text fields with help of a NSObjectController. The only problem I have so far: you always have to leave a text field before the NSObjectController updates the class with the current input.

This becomes a problem if the user doesn't leave a texfield and clicks on a Save/Submit Button right away. The class doesn't contain the current input. Always a bad thing.

I am looking for a way to avoid this. Like telling the NSObjectController to get the current input even if the user had exited the field. If this is possible I could put this command in the save-Method before saving and all would be fine.

+1  A: 

Take a look at this question: Can you manually implement Cocoa bindings?, i hope this helps you.

Nathan Campos
+1  A: 

Send a commitEditing message to your controller in the handler for the OK button. This will do what you're asking for. It's as simple as:

- (void)save:sender {
    if (![self.myObjectController commitEditing]) {
        // Handle error when object controller can't commit editing
    }

    // Other stuff
}
Alex
A: 

If you go to the text field's value binding and check the "Continuously Updates Value" option, that will cause the new value to be set on the model object each time the user changes it, i.e. once for each keystroke. That would ensure that the model had the correct value before closing the window, though it may be a bit overkill, depending on what the effects (if any) are of the value being set in your data model.

Brian Webster