2012-06-12 1 views
0

범위가 너무 큰 정수 값을 가지기 때문에 슬라이더를 사용하는 것이 비현실적입니다. 범위에서 정확한 값을 찾아 낼 수있는 감도가 없습니다. .설정 번들의 정수 값 키보드 입력에 대한 강력한 솔루션이 필요합니다.

대신 PSTextFieldSpecifier를 사용하여 키보드를 Numbers로 설정했습니다. 그러나 Settings.app에서 복사하여 붙여 넣기 기능을 사용하면 텍스트를 숫자 필드로 삽입 할 수 있습니다.

누구나 문제를 해결할 수 있습니까?

답변

1

난 항상 정의

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

루틴이 구현했습니다. 그들이 들어오는대로 문자를 모니터하고 숫자 만 허용합니다.

// 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]; 
} 

종료 할 때 자동으로 호출되는 루틴, I :

은 내가 UITextFieldDelegate의 아래에 요약 방법을의 UITextField를 사용하고, 구현 값 한도 검사를 수행했지만이 코드는 삭제 될 수 있습니다.

+0

그러나 이것은 Settings.app의 문제를 해결하지 못합니다. – Ben

+0

이것이 문제가된다면 설정을 로컬에 저장하고 앱 내부에서 로컬로 제어하는 ​​방법에 대해 크게 생각할 것입니다. 그런 다음 위의 내용을 구현하십시오. – trumpetlicks