【问题标题】:Setting TextField Focus on text removal将 TextField Focus 设置为删除文本
【发布时间】:2013-08-12 05:46:53
【问题描述】:

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

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

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。

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

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    试试这段代码,改变 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;
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

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

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

      希望对你有帮助。

      【讨论】:

      • 嗨 chandan,仅未选中,问题在于将文本字段设置为成为第一响应者时。它会自动清除它。
      • 请检查我更新的答案并更改 shouldChangeCharactersInRange 方法。如果仍然出现任何问题,请告诉我。谢谢
      • 这是由于 shouldChangeCharactersInRange 方法。请在 else if([string isEqualToString:@""]) 之前打印字符串值以检查其是否正常工作。
      【解决方案3】:

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

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

      - (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];
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        • 2021-12-23
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多