作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 6 个文本字段的 OTP 屏幕,当我点击特定的文本字段时,我需要光标不要移动到特定的文本字段。
我已经实现了自动将光标从一个文本字段移动到另一个文本字段的代码,但如果我触摸某个特定的文本字段(比如说第 4 个),光标就会移动到那里。
请帮我解决这个问题,在此先感谢您。
我的 OTP 屏幕:
我的代码:
func setDelegateAndTargetForAllTextFields() {
textField1.delegate = self
textField2.delegate = self
textField3.delegate = self
textField4.delegate = self
textField5.delegate = self
textField6.delegate = self
textField1.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
textField2.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
textField3.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
textField4.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
textField5.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
textField6.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControl.Event.editingChanged)
}
@IBAction func otpBtnTapped(_ sender: UIButton) {
if let text1 = textField1.text, let text2 = textField2.text, let text3 = textField3.text, let text4 = textField4.text,let text5 = textField5.text,let text6 = textField6.text {
if text1+text2+text3+text4+text5+text6 == ""{
showAlert(message: "Otp Cannot be empty!.")
textField1.text = ""
textField2.text = ""
textField3.text = ""
textField4.text = ""
textField5.text = ""
textField6.text = ""
textFieldIntermediateEditingDisabled()
}
else{
let setPinVC = self.storyboard?.instantiateViewController(withIdentifier: "pinVC") as! SetPinVC
self.navigationController?.pushViewController(setPinVC, animated: true)
}
}
}
@objc func textFieldDidChange(textField: UITextField) {
let text = textField.text
if text?.count == 1 {
switch textField{
case textField1:
//if textField2.isEnabled == false{
//textField2.isEnabled = true
//}
textField2.becomeFirstResponder()
case textField2:
//if textField3.isEnabled == false{
// textField3.isEnabled = true
// }
textField3.becomeFirstResponder()
case textField3:
//if textField4.isEnabled == false{
//textField4.isEnabled = true
//}
textField4.becomeFirstResponder()
case textField4:
//if textField5.isEnabled == false{
//textField5.isEnabled = true
//}
textField5.becomeFirstResponder()
case textField5:
// if textField6.isEnabled == false{
//textField6.isEnabled = true
//}
textField6.becomeFirstResponder()
default:
break
}
}
if text?.count == 0 {
switch textField {
case textField1:
//if textField1.isEnabled == false{
//textField1.isEnabled = true
//}
textField1.becomeFirstResponder()
case textField2:
// if textField1.isEnabled == false{
//textField1.isEnabled = true
//}
textField1.becomeFirstResponder()
case textField3:
//if textField2.isEnabled == false{
//textField2.isEnabled = true
//}
textField2.becomeFirstResponder()
case textField4:
//if textField3.isEnabled == false{
//textField3.isEnabled = true
//}
textField3.becomeFirstResponder()
case textField5:
//if textField4.isEnabled == false{
// textField4.isEnabled = true
//}
textField4.becomeFirstResponder()
case textField6:
//if textField5.isEnabled == false{
//textField5.isEnabled = true
//}
textField5.becomeFirstResponder()
default:
break
}
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 1
let currentString: NSString = textField.text! as NSString
let newString: NSString =
currentString.replacingCharacters(in: range, with: string) as NSString
return newString.length <= maxLength
}
最佳答案
您可以使用 textFieldShouldBeginEditing
来检查并允许/放弃 textField 开始像这样编辑
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true // edit the condition according to your requirement
}
编辑
如果你想检查并允许前一个文本字段是否有值,你可以做这样的事情,只需将所有文本字段放入一个数组中,顺序从一到等等
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let textfields : [UITextField] = [textfield1,textfield2,textfield3] // just to demonstrate
if (textField == textfield1) { return true }
var previousTextfield : UITextField?
for textfield in textfields{
if let pTextfiled = previousTextfield,let value = pTextfiled.text{
return value.isEmpty ? false : (textfield == textField)
}
previousTextfield = textfield
}
return false
}
Note: I didn't tested this code just added the answer according to the requirement let me know if this is what you wanted or something else.
谢谢。
关于ios - 如何在 swift 4 中禁用 otpscreen 中文本字段上光标的中间外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54473168/
我有一个带有 6 个文本字段的 OTP 屏幕,当我点击特定的文本字段时,我需要光标不要移动到特定的文本字段。 我已经实现了自动将光标从一个文本字段移动到另一个文本字段的代码,但如果我触摸某个特定的文本
我是一名优秀的程序员,十分优秀!