ios - 刚好输入一个数字后移至下一个 UITextField

标签 ios swift textfield

我想为客户 ID 制作一个登录屏幕,它接受数字键盘的数字输入,并在用户输入一个数字后移动到下一个 UITextField

我有五个 UITextField,第一个 UITextField 应该成为带有数字键盘的第一响应者,并且应该在不按回车键的情况下通过这些字段。四个 UITextField 是,

@IBOutlet weak var customerIdOne: UITextField!
@IBOutlet weak var customerIdTwo: UITextField!
@IBOutlet weak var customerIDThree: UITextField!
@IBOutlet weak var customerIdFive: UITextField!
@IBOutlet weak var customerIdFour: UITextField!

在按下登录按钮时,UITextField 中的所有值都应该连接起来。

@IBAction func loginButton(_ sender: Any) {
    custID = "\(customerIdOne.text!)\(customerIdTwo.text!)\(customerIDThree.text!)\(customerIdFour.text!)\(customerIdFive.text!)"
    print(custID)
}

我是初学者,我想知道是否有有效的方法来实现这一点。

目前,我使用带有 textFieldShouldReturn 委托(delegate)方法的标签

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if textField.tag == 1
    {
        customerIdOne.resignFirstResponder()
        customerIdTwo.becomeFirstResponder()
    }

    if textField.tag == 2
    {
        customerIdTwo.resignFirstResponder()
        customerIDThree.becomeFirstResponder()
    }
    if textField.tag == 3
    {
        customerIDThree.resignFirstResponder()
        customerIdFour.becomeFirstResponder()
    }
    if textField.tag == 4
    {
        customerIdFour.resignFirstResponder()
        customerIdFive.becomeFirstResponder()
    }
    if textField.tag == 5
    {
        customerIdFive.resignFirstResponder()
    }
    return false
}

最佳答案

第一步

创建 IBOutletCollections 并为每个文本字段设置 标签 以识别用户点击了哪个文本字段。

@IBOutlet var customerIdButtons: [UITextField]!

第二步

为文本域创建公共(public)扩展

extension yourViewController : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let newString = ((textField.text)! as NSString).replacingCharacters(in: range, with: string)
    if newString.count == 1 {
        textFieldShouldReturnSingle(textField, newString : newString)
        return false
    }
    return true
}
func textFieldShouldReturnSingle(_ textField: UITextField, newString : String)
{


    let nextTag: Int = textField.tag + 1
    textField.text = newString
    let nextResponder: UIResponder? = textField.superview?.viewWithTag(nextTag)
    if let nextR = nextResponder
    {
        // Found next responder, so set it.
        nextR.becomeFirstResponder()
    }
    else
    {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
        // call your method
    }
   }

}

最后获取所有客户ID,然后使用

@IBAction func loginButton(_ sender: Any) {
    var texts:  [String] = []
     customerIdButtons.forEach {  texts.append($0.text!)}
      custID = texts.reduce("", +)
     print(custID)
}

关于ios - 刚好输入一个数字后移至下一个 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691812/

相关文章:

iphone - iOs - iAd 可用性,iAd 服务器多久没有广告返回?

iOS 拍照并转换为 Base64 字符串

ios - 与当前时间的差异 - 使用 String 而不是 dateWithEra

javascript - 使用当前页面 URL 自动填充文本字段值

ios - NSCalendar ordinalityOfUnit 未正确返回星期几

ios - 在 iOS Firebase 远程配置中的应用程序启动过程中,是否会获取但未应用的值?

ios - 取消 NSOperation 完成 block

ios - 第二次登录并转到菜单后如何避免第二次查看(我的应用程序的展示)?

Flutter - TextField 失去焦点时失去值(value)

flash - 更改 TextField.text 时 Actionscript TextFormat 会重置