1

UITextfied に入力する文字数を制限しようとしていますが、ユーザーに 40.[0 から 9] から 250.[0 から 9] の範囲の数字を入力してもらいたいのですが、ユーザーは小数点以下 1 桁しか入力できず、また、複数の小数点を入力することもできません。これまでのところ、以下のコードをいくつか試しましたが、私の主張が明確になったことを願っています

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

    // allow backspace
    if (range.length > 0 && [string length] == 0) {
        return YES;
    }
    // do not allow . at the beggining
    if (range.location == 0 && [string isEqualToString:@"."]) {
        return NO;
    }   
    // set the text field value manually
    NSString *newValue = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    newValue = [[newValue componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""];
    textField.text = newValue;
    // return NO because we're manually setting the value
    return NO;
}

だから友達は私をさらに助けてください。

よろしくランジット

4

0 に答える 0