One Quickie


Restricting typed in text to just digits (NSTextView->General)
Create a subclass of NSNumberFormatter, add this method, and hook up the formatter to your text fields.
- (BOOL) isPartialStringValid: (NSString **) partialStringPtr
        proposedSelectedRange: (NSRangePointer) proposedSelRangePtr
               originalString: (NSString *) origString
        originalSelectedRange: (NSRange) origSelRange
             errorDescription: (NSString **) error
{
    NSCharacterSet *nonDigits;
    NSRange newStuff;
    NSString *newStuffString;

    nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    newStuff = NSMakeRange(origSelRange.location,
                           proposedSelRangePtr->location
                           - origSelRange.location);
    newStuffString = [*partialStringPtr substringWithRange: newStuff];

    if ([newStuffString rangeOfCharacterFromSet: nonDigits
            options: NSLiteralSearch].location != NSNotFound) {
        *error = @"Input is not an integer";
        return (NO);
    } else {
        *error = nil;
        return (YES);
    }

} // isPartialStringValid



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com