Binding NSTextField to NSNumber

I am trying to use NSTextField to input an integer user. The text field is bound to the NSNumber property, and in the setter method I clear the input value (make sure it is int) and set the property if necessary. I am sending willChangeValueForKey: and didChangeValueForKey :, but the user interface is not updated to new values ​​while this text field is still active.

So, for example, I can type “12abc” into the text box, which the setter method clears to “12”, but the text box still shows “12abc”.

I have the value "Continuous Update" set in the interface builder.

(I also noticed that the setter method gets an NSString, not an NSNumber. Is this normal?)

What is the correct way to connect NSTextField to NSNumber? What does the setter method for a property look like? How to prevent non-numeric values ​​from appearing in a text box?

+5
source share
4 answers

I am sending willChangeValueForKey: and didChangeValueForKey :, but the user interface is not updated to the new values ​​while the text field is still active.

To send these messages is very small. Typically, you can do the same job better and cleaner by implementing and using accessors (or, even better, properties). KVO will send you notifications when you do this.

, (, "12abc" ). .

, " " IB .

:

- (BOOL) validateMyValue:(inout NSString **)newValue error:(out NSError **)outError {
    NSString *salvagedNumericPart;
    //Determine whether you can salvage a numeric part from the string; in your example, that would be "12", chopping off the "abc".
    *newValue = salvagedNumericPart; //@"12"
    return (salvagedNumericPart != nil);
}

:

- (BOOL) validateMyValue:(inout NSString **)newValue error:(out NSError **)outError {
    BOOL isEntirelyNumeric;
    //Determine whether the whole string (perhaps after stripping whitespace) is a number. If not, reject it outright.
    if (isEntirelyNumeric) {
        //The input was @"12", or it was @" 12 " or something and you stripped the whitespace from it, so *newValue is @"12".
        return YES;
    } else {
        if (outError) {
            *outError = [NSError errorWithDomain:NSCocoaErrorDomain code: NSKeyValueValidationError userInfo:nil];
        }
        //Note: No need to set *newValue here.
        return NO;
    }
}

( , setter NSString, NSNumber. ?)

, , , formatter formatter NSNumber NSString .

+10

Theres Peter Hoseys , Id ( ).

/ NSTextField , , , , , . , - (void)controlTextDidChange:(NSNotification *)aNotification . . , controlTextDidChange.

:

- (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];
}
+2

, NSNumberFormatter , , , Just Work ™.

, NSNumberFormatter , , NSNumberFormatter , . , overriding - numberFromString: - .

NSValueTransformer NSNumbers , NSNumberFormatter, , .

+1

, - NSTextField.

Read about the formats on the Apple website: Using Formatters .

0
source

All Articles