需要一个强大的解决方案来设置包中的整数值的键盘输入

时间:2012-06-12 17:31:20

标签: iphone ios5 settings settings.bundle

我有一个跨越范围的整数值,使用滑块是不切实际的,因为它缺乏选择该范围内精确值的灵敏度。

我使用了PSTextFieldSpecifier,并将键盘设置为Numbers。但是,在Settings.app中,复制和粘贴功能允许将文本插入应该是数字字段的内容。

有没有人能解决这个问题?

1 个答案:

答案 0 :(得分: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];
}

在我的示例中,我确实执行了值限制检查,但可以删除此代码。