1

我有四个文本字段。用户只能在每个文本字段中输入一个数字。一旦用户输入单个数字,其焦点应该在下一个文本字段上。我已经完成了这部分并且工作正常。现在,我想要的是当我从文本字段中删除文本时,它的焦点应该移到上一个文本字段。我的意思是,如果我从第四个文本字段中删除文本(数字),那么它的焦点应该移到第三个文本字段。简而言之,在删除文本时,重点应该放在前一个文本字段上。

现在,我的问题是当我从文本字段中删除文本然后它的焦点移动到上一个文本字段但它清除了该文本字段的文本(我设置焦点的文本字段)。我想要的是文本不应该是删除焦点。

in .h file

IBOutlet UITextField *txtPinDigit1;
    IBOutlet UITextField *txtPinDigit2;
    IBOutlet UITextField *txtPinDigit3;
    IBOutlet UITextField *txtPinDigit4;
     UITextField *currentTextField;

in .m file

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField.tag==0)
    {
        currentTextField=txtPinDigit1;
    }
    else if(textField.tag==1)
    {
        currentTextField=txtPinDigit2;
    }
    else if(textField.tag==2)
    {
        currentTextField=txtPinDigit3;
        if(isDelete)
        {
            textField.text=digit3;
        }
    }
    else if(textField.tag==3)
    {
        currentTextField=txtPinDigit4;
    }

    return YES;
}


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

    if(([textField.text length]==1)&&(![string isEqualToString:@""]))
    {
        if(currentTextField==txtPinDigit1)
        {
            [txtPinDigit2 becomeFirstResponder];
        }
        else if(currentTextField==txtPinDigit2)
        {
            [txtPinDigit3 becomeFirstResponder];
        }
        else if(currentTextField==txtPinDigit3)
        {
            [txtPinDigit4 becomeFirstResponder];
        }
        else if(currentTextField==txtPinDigit4)
        {
            textField.text = [textField.text substringToIndex:MAXLENGTH-1];
            //[txtPinDigit4 resignFirstResponder];
            //[txtPinDigit1 becomeFirstResponder];
        }
    }
    else if([string isEqualToString:@""])
    {
        isDelete=YES;
        NSLog(@"replacementString:%@",string);
       // textField.text=string;
        if(currentTextField==txtPinDigit4)
        {
            textField.text=string;
            digit3=nil;
            [digit3 release];
            digit3=[[NSString alloc] initWithFormat:@"%i",[txtPinDigit3.text intValue]];

            [txtPinDigit3 becomeFirstResponder];
        }
        else if(currentTextField==txtPinDigit3)
        {
            textField.text=string;
            [txtPinDigit2 becomeFirstResponder];
        }
        else if(currentTextField==txtPinDigit2)
        {
            textField.text=string;
            [txtPinDigit1 becomeFirstResponder];
        }
        else if(currentTextField==txtPinDigit1)
        {
            textField.text=string;
           // [txtPinDigit1 resignFirstResponder];
            //[txtPinDigit1 becomeFirstResponder];
        }
    }

    return YES;
}

在上面的代码中定义了 MAXLENGTH=1。

任何帮助将不胜感激。提前致谢。

4

3 回答 3

3

试试这段代码,改变 shouldChangeCharactersInRange: Delegate Method

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

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if ([newString length] < 1) 
    {

        if (textField.tag==2)
        {
            [txtPinDigit1 performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
            //            txt2.text=@"";
        }
        else if (textField.tag==3)
        {

            [txtPinDigit2 performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
        }
        else if (textField.tag==4)
        {
            [txtPinDigit3 performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];

        }
        return YES;
    } else
    {
        // Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
        textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;

        if (textField.tag==1)
        {
            [textField resignFirstResponder];
            [txtPinDigit2 becomeFirstResponder];
        }
        else if (textField.tag==2)
        {
            [textField resignFirstResponder];
            [txtPinDigit3 becomeFirstResponder];
        }
        else if (textField.tag==3)
        {
            [textField resignFirstResponder];
            [txtPinDigit4 becomeFirstResponder];
        }
        return NO;
    }


}
于 2013-08-12T06:26:41.797 回答
0

通过这种方式检查长度并在 shouldChangeCharactersInRange 的开头放置一个断点

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
       int length = textField.text.length - range.length + string.length;

       return YES;
}

我希望它会帮助你。

于 2013-08-12T05:55:27.467 回答
0

在呼叫成为填写第一个数字的第一响应者之前,将输入的文本输入和响应者绑定到字典中......

在每个上一个/下一个条件下,从字典中检索信息...下面的示例代码用于一般的上一个/下一个自定义操作(虽然不是自动)..您只需要根据您的条件稍微更改逻辑更喜欢...

- (NSArray *) responders
{
    if (_responders)
        return _responders;

    NSArray *textInputs = EditableTextInputsInView([[UIApplication sharedApplication] keyWindow]);
    return [textInputs sortedArrayUsingComparator:^NSComparisonResult(UIView *textInput1, UIView *textInput2) {
        UIView *commonAncestorView = textInput1.superview;
        while (commonAncestorView && ![textInput2 isDescendantOfView:commonAncestorView])
            commonAncestorView = commonAncestorView.superview;

        CGRect frame1 = [textInput1 convertRect:textInput1.bounds toView:commonAncestorView];
        CGRect frame2 = [textInput2 convertRect:textInput2.bounds toView:commonAncestorView];
        return [@(CGRectGetMinY(frame1)) compare:@(CGRectGetMinY(frame2))];
    }];
}

在执行上一个/下一个自动选项卡之前,应该调用以下函数。

- (Void) selectAdjacentResponder: (UISegmentedControl *) sender
{
    NSArray *firstResponders = [self.responders filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIResponder *responder, NSDictionary *bindings) {
        return [responder isFirstResponder];
    }]];
    NSLog(@"%@",firstResponders);


    UIResponder *firstResponder = [firstResponders lastObject];
    NSInteger offset = sender.selectedSegmentIndex == 0 ? -1 : +1;
    NSInteger firstResponderIndex = [self.responders indexOfObject:firstResponder];

    NSInteger adjacentResponderIndex = firstResponderIndex != NSNotFound ? firstResponderIndex + offset : NSNotFound;
    UIResponder *adjacentResponder = nil;
    if (adjacentResponderIndex >= 0 && adjacentResponderIndex < (NSInteger)[self.responders count])
        adjacentResponder = [self.responders objectAtIndex:adjacentResponderIndex];

    [adjacentResponder becomeFirstResponder];
}

有点长,但这就是我所知道的一点:)..快乐的编码......

于 2013-08-12T06:26:34.313 回答