0

範囲が非常に広い整数値があるため、その範囲内の正確な値を選択する感度が不足しているため、スライダーを使用することは実際的ではありません。

代わりに PSTextFieldSpecifier を使用し、キーボードを Numbers に設定しました。ただし、Settings.app では、コピー アンド ペースト機能を使用して、数値フィールドにテキストを挿入できます。

誰かが問題の解決策を持っていますか?

4

1 に答える 1

1

私は常にこれをカスタムで実装してきました

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

ルーティーン。入ってくる文字を監視し、数字のみを受け入れます。

私は UITextField を使用し、以下に概説する UITextFieldDelegate メソッドを実装しました。

// This method enforces the textField to give up first responder and return
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

// This method copies the current UITextField' text into a string used for editing
// comparing, and for if a cancelation occurs we can replace with the original text
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    initialValueWhenEntering = [NSString stringWithString:textField.text];
    [initialValueWhenEntering retain];
}

// This routine enforces that only a single period or numerics be taken into the new
// value for the input
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    unsigned int stringLength = (unsigned int)[textField.text lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
    unsigned int counter;
    unsigned int periodCounter = 0;

    // Test to make sure there wasnt already some periods in the initial string
    for(counter = 0; counter < stringLength; counter++){
        if(   [textField.text characterAtIndex:(NSUInteger)counter] != '.' && 
            ( [textField.text characterAtIndex:(NSUInteger)counter] < '0' ||
              [textField.text characterAtIndex:(NSUInteger)counter] > '9' ) ){
            return NO;
        }else if( [textField.text characterAtIndex:(NSUInteger)counter] == '.' ){
            periodCounter++;
        }
    }

    stringLength = (unsigned int)[string lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
    for(counter = 0; counter < stringLength; counter++){
        if(   [string characterAtIndex:(NSUInteger)counter] != '.' && 
            ( [string characterAtIndex:(NSUInteger)counter] < '0' || [string characterAtIndex:(NSUInteger)counter] > '9' ) ){
            return NO;
        }else if( [string characterAtIndex:(NSUInteger)counter] == '.' ){
            periodCounter++;
        }
    }

    if( periodCounter <= 1 ){
        return YES;
    }else{
        return NO;
    }
}

最後に、テキストフィールドの編集が終了したときに自動的に呼び出されるルーチン

-(void)textFieldDidEndEditing:(UITextField *)textField{
    unsigned int stringLength = (unsigned int)[textField.text
                                lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
    unsigned int counter;

    if( 0 == stringLength ){
        textField.text = initialValueWhenEntering;

        float temperature = [textField.text floatValue];

        // Set the temperature of all element boxes
        for(counter = MIN_ELEMENT_INDEX; counter <= MAX_ELEMENT_INDEX; counter++){
            [periodicButtons[counter] setOutputType:myGraphType aValue:temperature];
        }
    }else if( 1 == stringLength ){
        if( [textField.text characterAtIndex:(NSUInteger)0] == '.' ){
            textField.text = initialValueWhenEntering;
        }

        float temperature = [textField.text floatValue];

        textField.text = [NSString stringWithFormat:@"%.2f", temperature];

        // Set the temperature of all element boxes
        for(counter = MIN_ELEMENT_INDEX; counter <= MAX_ELEMENT_INDEX; counter++){
            [periodicButtons[counter] setOutputType:myGraphType aValue:temperature];
        }
    }else{ // Should be a semi-valid number at this point
        float temperature = [textField.text floatValue];

        if( temperature > 5900.0 ){
            temperature = 5900.0;
        }

        textField.text = [NSString stringWithFormat:@"%.2f", temperature];

        // Set the temperature of all element boxes
        for(counter = MIN_ELEMENT_INDEX; counter <= MAX_ELEMENT_INDEX; counter++){
            [periodicButtons[counter] setOutputType:myGraphType aValue:temperature];
        }
    }

    [initialValueWhenEntering release];
}

私の例では、値制限チェックを実行しましたが、このコードは削除される可能性があります。

于 2012-06-12T17:48:32.013 に答える