如何在swift 4中禁用otp屏幕中文本字段上光标的中间外观
Posted
技术标签:
【中文标题】如何在swift 4中禁用otp屏幕中文本字段上光标的中间外观【英文标题】:how the disable the intermediate appearance of cursor on textFields in a otpscreen in swift 4 【发布时间】:2019-02-01 05:04:41 【问题描述】:我有一个带有 6 个文本字段的 OTP 屏幕,当我点击特定的文本字段时,我需要光标不要移动到特定的文本字段。
我已经实现了自动将光标从一个文本字段移动到另一个文本字段的代码,但是如果我触摸特定的文本字段(比如说第四个),光标就会移动到那里。
请帮我解决这个问题,并提前致谢。
我的 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
【问题讨论】:
请分享您的代码,然后我们提供如何禁用其他文本字段的想法。 您需要哪种类型的验证,我的意思是如果 !txt.text.isEmpty if tap its go the specific field or else 【参考方案1】:您可以使用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
注意:我没有测试此代码只是根据要求添加答案让我知道这是您想要的还是其他。
谢谢。
【讨论】:
我的要求只是光标不应该在中间文本字段上。所以你能说一下条件吗.. "intermediate" 没有说明实际要求?你的意思是你不想允许如果之前的文本字段是空的,对吧? 编辑了答案,请尝试让我知道@vivekananda 只有在 textfield1 中没有文本但如果我们在 textField2 中有一些文本并且如果我点击 textfield4 时它会移动到那里 现在当我第一次进入 otp 时,它会停在第二个 textField 本身以上是关于如何在swift 4中禁用otp屏幕中文本字段上光标的中间外观的主要内容,如果未能解决你的问题,请参考以下文章
如何在stackview文本字段中快速获取otp的动态数字键盘中的数字
如何在 swift 中为一个文本字段禁用 DatePicker 交互(用于选择日期)“