【问题标题】:How to make UITextField with limitation如何使 UITextField 受到限制
【发布时间】:2015-05-05 07:28:15
【问题描述】:
  1. phoneTextField;
  2. emailTextField;

如果我开始输入 phoneTextField,emailTextField 不允许字符。 如果 phoneTextField 长度将变为 0 意味着只有 emailTextField 将允许字符。

与开始输入emailTextField 时一样,phoneTextField 不允许字符。 如果 emailTextFile 长度变为 0 表示只有 phoneTextField 将允许字符。

请分享示例代码..

【问题讨论】:

  • 你用谷歌搜索过吗?
  • 我试了,但不正确..

标签: ios iphone xcode6 uitextfield uitextfielddelegate


【解决方案1】:

您可以使用此委托方法来执行此操作

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == phoneTextFiled && (emailTextField.text.lenght == 0))
        return YES;

    else if(textField == emailTextField && (phoneTextFiled.text.lenght == 0))
        return YES;

    return NO;

}

【讨论】:

    【解决方案2】:

    使用文本字段委托

    将两个文本字段 delegate 设置为 self

    textFieldDidBeginEditing: 中禁用其他文本字段。

    然后在- textFieldDidEndEditing:检查这个文本字段的长度。如果长度==0,设置其他文本字段启用

    Swift 示例

    我碰巧有两个名为用户名和密码的文本字段。直接替换

    func textFieldDidBeginEditing(textField: UITextField) {
        if textField == usernameTextfield{
            passwordTextField.enabled = false
        }
        if textField == passwordTextField{
            usernameTextfield.enabled = false
        }
    }
    func textFieldDidEndEditing(textField: UITextField) {
        if count(textField.text)==0{
            if textField == usernameTextfield{
                passwordTextField.enabled = true
            }
            if textField == passwordTextField{
                usernameTextfield.enabled = true
            }
        }
    }
    

    【讨论】:

    • 我确实认为,如果您启用文本字段但用户无法输入任何内容。这不是用户友好的
    【解决方案3】:

    使用文本文件的委托方法并根据您的要求进行更改,如下面的示例..

     - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
        {
            if (textField==self.txtNewPassword || (textField == self.txtReNewPassword))
            {
                NSString *updatedText = [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByReplacingCharactersInRange:range withString:string];
                if (updatedText.length > CHARACTER_COUNT)
                {
                    return NO;
                }
            }
            return YES;
        }
    

    【讨论】:

      【解决方案4】:

      做以下事情你会得到渴望的输出。这里EnableDesable 将满足您的要求。

      - (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
      
      
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
              if (textField.tag == kTextFieldTagPassword && textField.text.length!=0)
              {
                  emailTextField.enable = NO;
      
              }
              else{
                  emailTextField.enable = YES;
              }
      
              if (textField.tag == kTextFieldTagEmail && textField.text.length!=0)
              {
                  phoneTextField.enable = NO;
      
              }
              else{
                  phoneTextField.enable = YES;
              }
      
      
          });
      
      
          return YES;
      }
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        相关资源
        最近更新 更多