我希望在当前文本字段中输入 2 个字符后,文本字段的焦点立即转移到下一个文本字段。我正在使用此代码,但我认为 return 语句的放置会导致某些问题,例如:1)在最后一个文本字段中,它没有将其限制为 2 个字符。2) 将焦点转移到下一个文本字段后如果我回到上一个文本字段,那么它也允许我输入两个以上的字符。3)由于在 textfieldDidBegin 编辑方法中添加了“self.firstResponder = textfield”,我的其他文本字段变得无响应,即不允许我输入任何文本。这是我所有三种方法的代码。:-这是shouldCharctersInRange 方法中的代码:
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSString * newString = [textField.text stringByReplacingCharactersInRange:range
withString:string];
if (string.length==0) {
return YES;
}
if (textField == txt_mins) {
return [[AppDelegate sharedInstance] setNumberFormat:textField newString:newString
IntegerLimit:5 FractionLimit:2 NumberType:2];
}
else if (textField == txt_units){
return [[AppDelegate sharedInstance] setNumberFormat:textField newString:newString
IntegerLimit:5 FractionLimit:2 NumberType:2];
}
// Below logic is for All 4 Modifer Textfields
// we are restricting the user to enter only max 2 characters in modifier textfields
and
also automatically
// converting each entered character to the uppercase string.
if (textField==txt_modifier1 || textField==txt_modifier2 || textField==txt_modifier3 ||
textField==txt_modifier4) {
// This condition is restricting the user from entering the special characters in the
modifier textfield.
if ([string rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet]
invertedSet]].location != NSNotFound) {
// There are non-alphanumeric characters in the replacement string
return NO;
}
//Added by Shikhar
// dated: 18 June, 2013
else{
NSString *result = [textField.text stringByReplacingCharactersInRange:range
withString:string.uppercaseString];
// this condition is automatically transferring the focus to the next textfield as
soon we type two characters in the\
// the current textfield.
textField.text = result;
if (result.length == 2){
[self toggleTextfield];
}
return NO;
}
}
return YES;
}
- (void) toggleTextfield
{
NSInteger nextTag = self.firstResponder.tag;
nextTag += 1;
UITextField *nextTextField = (UITextField *)[self.view viewWithTag:nextTag];
if (nextTextField)
{ [nextTextField setUserInteractionEnabled:YES];
[nextTextField becomeFirstResponder];
}
和 textfieldDidBegin 编辑的代码是这样的:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.firstResponder = textField;
[textField setInputAccessoryView:tb_doneToolbar];
txt_currentFocusedTextfield = textField;
CGRect textFieldRect =[self.view.window convertRect:textField.bounds
fromView:textField];
CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y +1.0*textFieldRect.size.height;
CGFloat numerator =midline - viewRect.origin.y- MINIMUM_SCROLL_FRACTION *
viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)*
viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
animatedDistance = floor(162.0 * heightFraction);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
如果有人可以提供帮助,我们将不胜感激。