1

我希望在当前文本字段中输入 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];

}

如果有人可以提供帮助,我们将不胜感激。

4

2 回答 2

1

我的代码做了类似的事情;不同的是,它inputAccessoryView通过UIKeyboard. UITextField首先,您需要实现一个方法,该方法返回视图控制器上的 s 数组。

- (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是一个 custom 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;
        }

    }
}
}

forwardDirectionNO如果要转到上一个键盘,并且要YES切换到下一个键盘,则需要。arrTextFields是 VC 上的数组UITextFields,使用第一种方法调用这个作为第一个参数。

于 2013-06-20T12:06:06.080 回答
0

第一个问题的解决方案是

//将此代码添加到您检查toggleTextfield的代码上方

if(textField==txt_modifier4 && result.length ==2)
   [self resignFirstResponder]

问题:当其他文本字段变得无响应时?

于 2013-06-20T07:00:19.857 回答