出于验证目的,我有四个UITexField
个单独的字符,每个字符一个
它们如下所示:
(-----)
我需要一种方法,当用户在一个文本字段中键入内容时,它将直接转到下一个文本字段,而无需单击另一个文本字段。
或者,如果还有其他方法可以实现此功能,请告诉我
答案 0 :(得分:0)
不是只显示所有文本字段,而是只使用1个文本字段(仅在代码中-不在视图层次结构中),它将从用户那里获取整个代码。并在屏幕上显示4个按钮-谁的标题将是基于索引的文本字段的字符。这样,您就可以实现它。
答案 1 :(得分:0)
好问题的老板 我有一个解决您的问题的方法。 尝试这个... 注意*根据您的要求,您可以通过textFieldDidChange方法来操作文本字段...
class ViewController: UIViewController ,UITextFieldDelegate {
@IBOutlet var text1 : UITextField!
@IBOutlet var text2 : UITextField!
@IBOutlet var text3 : UITextField!
@IBOutlet var text4 : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//TAG FOR IDENTITY
text1.tag = 1
text2.tag = 2
text3.tag = 3
text4.tag = 4
text1.becomeFirstResponder()
text1.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
text2.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
text3.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
text4.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
}
@objc func textFieldDidChange(_ sender : UITextField){
if sender.tag == 1{
text1.resignFirstResponder()
text2.becomeFirstResponder()
}else if sender.tag == 2{
text2.resignFirstResponder()
text3.becomeFirstResponder()
}else if sender.tag == 3{
text3.resignFirstResponder()
text4.becomeFirstResponder()
}else if sender.tag == 4{
text4.resignFirstResponder()
}else{
print("Done")
}
}
}
答案 2 :(得分:0)
您可以使用4个文本字段,然后在editChanged上移至下一个文本字段,其格式如下:
@IBOutlet weak var code1: UITextField!
@IBOutlet weak var code2: UITextField!
@IBOutlet weak var code3: UITextField!
@IBOutlet weak var code4: UITextField!
func addTargets() {
code1.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
code2.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
code3.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
code4.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
}
@objc func editingChanged(sender: UITextField) {
switch (sender) {
case code1:
code2.becomeFirstResponder()
case code2:
code3.becomeFirstResponder()
case code3:
code4.becomeFirstResponder()
default:
// your code input would be done here, you could handle for example code verification
break;
}
}
答案 3 :(得分:0)
首先将textField保持在UIViewController类中全局访问。
确保您的UIViewController符合委托UITextFieldDelegate。
然后在UIViewController中为每个textField设置委托:
textField.delegate = self
对于以下代码,我以下面的4个文本字段为例。用户从左到右输入电话号码。 textField名称依次为ext,areaCode,middleThree,finalFour。
当用户按下返回键时移至下一个字段
// Return text field upon pressing return
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == self.ext {
self.areaCode?.becomeFirstResponder()
} else {
self.areaCode?.resignFirstResponder()
}
if textField == self.areaCode {
self.middleThree?.becomeFirstResponder()
} else {
self.middleThree?.resignFirstResponder()
}
if textField == self.middleThree {
self.finalFour?.becomeFirstResponder()
} else {
self.finalFour?.resignFirstResponder()
}
if textField == self.finalFour {
self.finalFour?.resignFirstResponder()
}
return true
}
字符计数完成后,移至下一个字段(例如,用户在文本框中输入4个字符后,请转到下一个字段。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString: String) -> Bool {
var characterCountLimit = 0;
switch textField {
case self.ext:
characterCountLimit = 3
case self.areaCode:
characterCountLimit = 3
case self.middleThree:
characterCountLimit = 3
case self.finalFour:
characterCountLimit = 4
default:
characterCountLimit = 1000
}
// Determine how many characters in the string after the change happens.
let startingLength = textField.text?.count ?? 0
let lengthToAdd = replacementString.count
let lengthToReplace = range.length
// This might be called when the user presses backspace, but the count is has reached the limit. Thus, we have to determine the new length.
let newLength = startingLength + lengthToAdd - lengthToReplace
// If the user did not press backspace, but continued typing, move to the next field. Take the most recently typed character and enter it as text in the new field and remove it from the old field.
if newLength > characterCountLimit {
switch textField {
case self.ext:
self.areaCode.text = String(describing: (replacementString.last)!)
self.areaCode?.becomeFirstResponder()
case self.areaCode:
self.middleThree.text = String(describing: (replacementString.last)!)
self.middleThree?.becomeFirstResponder()
case self.middleThree:
self.finalFour.text = String(describing: (replacementString.last)!)
self.finalFour?.becomeFirstResponder()
case self.finalFour:
break
// Do nothing. This is handled by the editing change delegate method.
default:
break
}
} else if replacementString.isEmpty && range.lowerBound == 0 && range.upperBound == 1 {
// If the user is pressing backspace and you want to return to the previous field
switch textField {
case self.finalFour:
self.finalFour.text = ""
self.middleThree?.becomeFirstResponder()
case self.middleThree:
self.middleThree.text = ""
self.areaCode?.becomeFirstResponder()
case self.areaCode:
self.areaCode.text = ""
self.ext?.becomeFirstResponder()
default:
break
}
}
return newLength <= characterCountLimit
}