0

3つのUItextfieldを1つの数値に制限するのが好きです。このコードを使用しますが、機能しません。

コード:

- (BOOL)textField:(UITextField *)numeroIngresa1
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 if (range.location >= 1)
 return NO; // return NO to not change text
 return YES;
}
4

1 に答える 1

3
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

    NSNumber* candidateNumber;

    NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    range = NSMakeRange(0, [candidateString length]);

    [numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];

    if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {

        return NO;
    }
    else 
    {
        return YES;
    }
}

ソース:iPhoneUITextFieldのみ整数

于 2012-04-15T02:06:52.993 に答える