输入一个数字后,移动到下一个UITextField

时间:2018-04-06 11:29:09

标签: 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
}

2 个答案:

答案 0 :(得分:2)

<强>步骤1

为每个文本字段创建IBOutletCollections并设置标记,以标识用户点击的文本字段。

@IBOutlet var customerIdButtons: [UITextField]!

<强>步骤-2

为textfield创建公共范围

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)
}

答案 1 :(得分:2)

您可以使用EditingChanged事件查看输入一个文本的时间。

enter image description here

然后,您可以使用viewWithTag方法查找要制作的下一个文本字段FirstResponder。以下是您需要为自己的目的编写的完整代码。

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.viewWithTag(1)?.becomeFirstResponder()
}

@IBAction func editingChanged(_ sender: UITextField) {
    if let nextTextField = self.view.viewWithTag(sender.tag + 1) {
        nextTextField.becomeFirstResponder()
    }
}

结果如下:

enter image description here

然后在按钮操作中,您可以获得如下文字:

@IBOutlet var customerIDTextFields: [UITextField]!  //IBOutletCollections of all textfields

@IBAction func loginButtonTapped(_ sender: UIButton) {
    var customerIDString = ""
    self.customerIDTextFields.forEach { (singleCustomerIDTextField) in
        customerIDString.append(singleCustomerIDTextField.text!)
    }
    print(customerIDString)
}