【问题标题】:Auto switching between UITextFields with Swift使用 Swift 在 UITextField 之间自动切换
【发布时间】:2017-06-14 05:45:27
【问题描述】:

我有 4 个 UITextFields 在一个 UIView 中,如下所示:

每个 UITextField 限制为 4 个字符。我想实现UITextFields 之间的自动切换,每个UITextField 的计数字符。

我使用shouldChangeCharactersIn 作为限制字符:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let maxLength = 4
        var newString = String()
        let currentString: NSString = textField.text! as NSString
        newString = currentString.replacingCharacters(in: range, with: string)

        return newString.length <= maxLength
    }

这是我的开关工具:

func textFieldDidChange(_ textField: UITextField){

    let text = textField.text

    if text?.utf16.count == 4 {
        switch textField {
        case firstPartTextField:
            secondPartTextField.becomeFirstResponder()
        case secondPartTextField:
            thirdPartTextField.becomeFirstResponder()
        case thirdPartTextField:
            fourthPartTextField.becomeFirstResponder()
        default:
            break
        }
    }
}

我的问题是在删除文本时切换到以前的UITextField,当UITextField 为空时,我无法检测到退格事件。

【问题讨论】:

标签: ios iphone swift uitextfield uitextfielddelegate


【解决方案1】:

在 viewDidLoad() 中为 textField 添加目标

firstTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    secTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    thirdTxt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
    fourTXt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)

比创建选择器方法

func textFieldDidChange(textField: UITextField){

    let text = textField.text

    if text?.utf16.count == 1{
        switch textField{
        case firstTxt:
            secTxt.becomeFirstResponder()
        case secTxt:
            thirdTxt.becomeFirstResponder()
        case thirdTxt:
            fourTXt.becomeFirstResponder()
        case fourTXt:
            fourTXt.resignFirstResponder()
        default:
            break
        }
    }else{

    }
}

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
  • 和上面一样,每个 UITextField 都有一个选择器,它自己调用,当文本发生变化时,它会计算字符,当它满足条件时,它将变为下一个 UITextField
【解决方案2】:

制作所有文本字段的出口集合并像这样配置它们:

出口收藏:
@IBOutlet var textfields: [UITextField]!
在 viewDidLoad 中添加:
for (i, tf) in textfields.enumerated() {
    tf.layer.cornerRadius = 5
    tf.clipsToBounds = true
    tf.tag = i
    tf.delegate = self
}

在目标函数中,您将以这种方式限制计数:

func textFieldDidChange(textField: UITextField) {
       let text = textField.text!
       if text.utf16.count >= 4 {
           if textField.tag < 4 {
               codeTextfields[textField.tag + 1].becomeFirstResponder()
           }
           else {
               textField.resignFirstResponder()
           }
       }
}

这将改变您的委托方法之一:

func textFieldDidBeginEditing(_ textField: UITextField) {
        textField.text = ""
}

以下回答的精炼版:How to move cursor from one text field to another automatically in swift ios programmatically?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多