Cocoa -Bindings: update NSObjectController manually?

In my little cocoa app, I have bound class properties to some text fields using NSObjectController. The only problem I've run into so far is that you always need to leave the textbox before NSObjectController updates the class with the current input.

This becomes a problem if the user does not leave the text fields and immediately presses the "Save / Submit" button. The class does not contain the current input. It's always bad.

I am looking for a way to avoid this. As well as telling NSObjectController to get the current input even if the user has left the field. If possible, I can put this command in save-Method before saving and everything will be fine.

+2


source to share


3 answers


Send a message commitEditing

to your controller in the OK button handler. This will do what you ask for. It's as simple as:



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

    // Other stuff
}

      

+1


source


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



+1


source


If you go to the text box value

and check the "Always update" option, this will cause a new value to be set on the model object every time the user changes it, ie. once for each keystroke. This would ensure that the model value is correct before the window is closed, although this might be a little overkill, depending on what effects (if any) have the value specified in your data model.

0


source







All Articles