我的tableView单元格内有很多textFields。我想通过单击键盘上的返回按钮在它们之间切换。我知道如何在独立的textField之间切换,而不是在tableView单元格之间切换。
答案 0 :(得分:-1)
这是我的方法: 1-将UITextFieldDelegate添加到您的类 2-将您的文本字段分组到一个数组中 3-遍历数组项并向其注册委托 4-实现textFieldShouldReturn方法
private func prepareTextFields() {
textFields.append(nameTextField)
textFields.append(surnameTextField)
textFields.append(addressTextField)
textFields.append(cityTextField)
textFields.append(stateTextField)
textFields.append(zipTextField)
for tf in textFields {
tf.delegate = self
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == nameTextField {
textField.resignFirstResponder()
surnameTextField.becomeFirstResponder()
} else if textField == surnameTextField {
textField.resignFirstResponder()
phoneTextField.becomeFirstResponder()
} else if textField == addressTextField {
textField.resignFirstResponder()
cityTextField.becomeFirstResponder()
} else if textField == cityTextField {
textField.resignFirstResponder()
stateTextField.becomeFirstResponder()
} else if textField == stateTextField {
textField.resignFirstResponder()
zipTextField.becomeFirstResponder()
} else if textField == zipTextField {
textField.resignFirstResponder()
}
return true
}