난 항상 정의
- (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를 사용하고, 구현 값 한도 검사를 수행했지만이 코드는 삭제 될 수 있습니다.
그러나 이것은 Settings.app의 문제를 해결하지 못합니다. – Ben
이것이 문제가된다면 설정을 로컬에 저장하고 앱 내부에서 로컬로 제어하는 방법에 대해 크게 생각할 것입니다. 그런 다음 위의 내용을 구현하십시오. – trumpetlicks