One Quickie


Only allowing numbers in a text field (UITextField->General)
Sometimes you need a more general keyboard for a numeric-only text field. You can do something like this in a delegate method:
- (BOOL) textField: (UITextField *) textField
  shouldChangeCharactersInRange: (NSRange) range
              replacementString: (NSString *) string {
    NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range
                                          withString: string];
    if (resultingString.length == 0) return YES;
    NSScanner *scanner = [NSScanner scannerWithString: resultingString];

    float throwaway;
    BOOL scansFloat = [scanner scanFloat: &throwaway];
    BOOL atEnd = [scanner isAtEnd];

    return scansFloat && atEnd;

} // shouldChangedCharacersInRanges
(thanks to Frank Shearer, from a SO post)



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

webmonster@borkware.com