我希望只要在当前文本字段中输入2个字符,文本字段的焦点就会转移到下一个文本字段。我正在使用此代码,但我认为return语句的位置导致某些问题,例如:1)在最后一个文本字段中,它不会将其限制为2个字符。 2)将焦点移动到下一个文本字段后如果我回到上一个文本字段,那么它也允许我输入两个以上的字符。 3)由于在textfieldDidBegin编辑方法中添加了“self.firstResponder = textfield”,我的其他Textfields变得没有响应,即不允许我输入任何文本。这是我的所有三种方法的代码。: - 这是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];
}
如果有人可以提供帮助,那么我们将非常感激。
答案 0 :(得分:1)
我的代码做了类似的事情;不同的是它通过inputAccessoryView
上UIKeyboard
上的下一个/上一个按钮切换文本字段。首先,您需要实现一个方法,该方法在视图控制器上返回UITextField
的数组。
- (NSArray *)arrayOfTextFieldsOnTheVC{
//Return an array of all the desired textfields on the VC
return [NSArray arrayWithObjects: self.textField1, self.textField2, nil];
}
此时我正在使用两种不同的方法
- (void)tappedNextOnkeybardExtensionView:(KeyboardExtensionView *)keyboardExtensionView;
- (void)tappedPreviousOnkeybardExtensionView:(KeyboardExtensionView *)keyboardExtensionView;
KeyboardExtensionView
是自定义UIView
,我将其用作键盘的inputAccessoryView
。当用户点击下一个或上一个时,我正在调用下面的方法。您需要另一种机制来调用该方法(我认为您应该覆盖textField:shouldChangeCharactersInRange),它在给定的文本字段之间进行迭代:
- (void)setNextTextFieldFirstResponderFromTextFieldsArray:(NSArray *)arrTextFields inForwardDirection:(BOOL)forwardDirection{
for ( int i = 0; i < [arrTextFields count] ; i ++ ){
UITextField *tfd = [arrTextFields objectAtIndex:i];
if ( [tfd isFirstResponder] ){
if (i == 0 && !forwardDirection){
// Tapped previous on first text field
break;
}
else if (i == [arrTextFields count] - 1 && forwardDirection){
//Tapped next on last text field
break;
}
else if (forwardDirection){
[[arrTextFields objectAtIndex:i+1] becomeFirstResponder];
break;
}
else if (!forwardDirection){
[[arrTextFields objectAtIndex:i-1] becomeFirstResponder];
break;
}
}
}
}
如果您想使用之前的键盘,则需要<forwardDirection
NO
;如果您想切换到下一个键盘,则需要YES
。 arrTextFields
是VC上的UITextFields
数组,使用第一个方法来调用此方法作为第一个参数。
答案 1 :(得分:0)
第一个问题的解决方案是
//将此代码添加到您检查toggleTextfield
的代码上方if(textField==txt_modifier4 && result.length ==2)
[self resignFirstResponder]
问题: 当其他textFields变得没有响应时?