单击键盘中的 Return 键时 UITextField 如何移动到下一个 TextField
How UITextField move to next TextField When Click on Return Key in Keyboard
我正在使用这段代码来移动 UITextField,它正在工作,但我对这段代码不满意,我想要(当我单击 return 键然后光标移动到下一个 UITextField 时)这个功能在我的注册中或任何人都可以建议我的登录表格。
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: -10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: -10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: -10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: -80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: -80, up: true)
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: 10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: 10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: 10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: 80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: 80, up: true)
default:
break
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
为了在用户按下键盘上的 Next 时自动将光标移动到下一个数据输入字段,您需要从当前字段中 resignFirstResponder 并使用 becomeFirstResponder 将其分配给下一个字段
if self.emaillabel.isEqual(self.anotherTextField)
{
self.anotherTextField.becomeFirstResponder()
}
1- 为情节提要中的每个文本字段放置一个标签编号
2- 实现 textField 委托函数:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 1 { //say this is txtFldSponsorID
txtFldFullName.becomeFirstResponder()
}
return true
}
3- 将故事板中的 return 键类型更改为 "next" 而不是 "return",或者使用代码:
txtFldSponsorID.returnKeyType = .next
4- 不要忘记设置 delegate = self
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == txtFldSponsorID {
txtFldFullName.becomeFirstResponder()
} else if textField == txtFldFullName {
txtFldEmail.becomeFirstResponder()
} else if textField == txtFldEmail {
txtFldMobile.becomeFirstResponder()
} else if textField == txtFldMobile {
txtFldAddress.becomeFirstResponder()
} else {
txtFldCity.resignFirstResponder()
}
return true
}
您可以使用上述 UITextField Delegate 方法跳转到下一个 UItextField。
我正在使用这段代码来移动 UITextField,它正在工作,但我对这段代码不满意,我想要(当我单击 return 键然后光标移动到下一个 UITextField 时)这个功能在我的注册中或任何人都可以建议我的登录表格。
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: -10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: -10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: -10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: -80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: -80, up: true)
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
switch textField {
case txtFldSponsorID:
moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
case txtFldFullName:
moveTextfield(textfield: txtFldFullName, moveDistance: 10, up: true)
case txtFldEmail:
moveTextfield(textfield: txtFldEmail, moveDistance: 10, up: true)
case txtFldMobile:
moveTextfield(textfield: txtFldMobile, moveDistance: 10, up: true)
case txtFldAddress:
moveTextfield(textfield: txtFldAddress, moveDistance: 80, up: true)
case txtFldCity:
moveTextfield(textfield: txtFldCity, moveDistance: 80, up: true)
default:
break
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
为了在用户按下键盘上的 Next 时自动将光标移动到下一个数据输入字段,您需要从当前字段中 resignFirstResponder 并使用 becomeFirstResponder 将其分配给下一个字段
if self.emaillabel.isEqual(self.anotherTextField)
{
self.anotherTextField.becomeFirstResponder()
}
1- 为情节提要中的每个文本字段放置一个标签编号
2- 实现 textField 委托函数:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 1 { //say this is txtFldSponsorID
txtFldFullName.becomeFirstResponder()
}
return true
}
3- 将故事板中的 return 键类型更改为 "next" 而不是 "return",或者使用代码:
txtFldSponsorID.returnKeyType = .next
4- 不要忘记设置 delegate = self
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == txtFldSponsorID {
txtFldFullName.becomeFirstResponder()
} else if textField == txtFldFullName {
txtFldEmail.becomeFirstResponder()
} else if textField == txtFldEmail {
txtFldMobile.becomeFirstResponder()
} else if textField == txtFldMobile {
txtFldAddress.becomeFirstResponder()
} else {
txtFldCity.resignFirstResponder()
}
return true
}
您可以使用上述 UITextField Delegate 方法跳转到下一个 UItextField。