I\'m trying to use an NSTextField for integer user input. The text field is bound to an NSNumber property and in the setter method I cleanup the input value (make sure it\'s
There’s an important comment on Peter Hosey’s excellent answer that I’d like to bring up to the top level (because I missed it on my first pass).
If you want to validate / modify the NSTextField each time a character is entered, rather than only when the user submits the field, then you can’t get what you want from bindings alone. You need to assign a delegate to the text field, and then implement - (void)controlTextDidChange:(NSNotification *)aNotification in the delegate. This will get called every time the text changes. If you want, you can call the value validator in controlTextDidChange.
For example:
- (void)controlTextDidChange:(NSNotification *)aNotification
{
NSError *outError;
NSControl *textField = [aNotification object];
NSString *myText = [textField stringValue];
// myObject is the model object that owns the text in question
// the validator can modify myText by reference
[myObject validateValue:&myText error:&outError]];
// update the NSNextField with the validated text
[postingObject setStringValue:myText];
}