如何在 Swift 中自动移动到下一个 UITextField
Posted
技术标签:
【中文标题】如何在 Swift 中自动移动到下一个 UITextField【英文标题】:How to move to the next UITextField automatically in Swift 【发布时间】:2018-01-18 02:04:20 【问题描述】:我有 2 个带有 NumberPad 键盘类型的文本字段
@IBOutlet weak var ourTextField: UITextField!
@IBOutlet weak var forThemTextField: UITextField!
我想在ourTextField中输入两个数字后自动移动到另一个文本字段(从ourTextField到forThemTextField),然后在转到另一个文本字段(forThemTextField)并输入2个数字后,我希望键盘自动隐藏
我添加了一个条件,通过此代码在我的 textFields 中只接受两个数字:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
let lengthsDictionary = [ourTextField : 2, forThemTextField: 2]
guard let length = lengthsDictionary[textField] else
return true
let currentCharacterCount = textField.text?.count ?? 0
if (range.length + range.location > currentCharacterCount)
return false
let newLength = currentCharacterCount + string.count - range.length
return newLength <= length
【问题讨论】:
【参考方案1】:使用shouldChangeCharactersIn
的UITextFieldDelegate
方法并用键盘字符串收听
使用 textFields 的标签,例如标签 1 和标签 2
作为
class TestingViewController: UIViewController
@IBOutlet weak var firstTextField: UITextField!
@IBOutlet weak var secondTextField: UITextField!
override func viewDidLoad()
super.viewDidLoad()
firstTextField.delegate = self
secondTextField.delegate = self
firstTextField.tag = 1
secondTextField.tag = 2
firstTextField.keyboardType = .numberPad
secondTextField.keyboardType = .numberPad
extension TestingViewController: UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
return textField.shouldChangeCustomOtp(textField: textField, string: string)
extension UITextField
func shouldChangeCustomOtp(textField:UITextField, string: String) ->Bool
//Check if textField has two chacraters
if ((textField.text?.count)! == 1 && string.count > 0)
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil)
nextResponder = textField.superview?.viewWithTag(1);
textField.text = textField.text! + string;
//write here your last textfield tag
if textField.tag == 2
//Dissmiss keyboard on last entry
textField.resignFirstResponder()
else
///Appear keyboard
nextResponder?.becomeFirstResponder();
return false;
else if ((textField.text?.count)! == 1 && string.count == 0) // on deleteing value from Textfield
let previousTag = textField.tag - 1;
// get prev responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil)
previousResponder = textField.superview?.viewWithTag(1);
textField.text = "";
previousResponder?.becomeFirstResponder();
return false
return true
输出:
【讨论】:
【参考方案2】:In viewDidLoad :-
ourTextField?.addTarget(self, action: #selector(CalculatorViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
forThemTextField?.addTarget(self, action: #selector(CalculatorViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
//create function
func textFieldDidChange(_ textField: UITextField)
if textField == ourTextField
if (textField.text.count)! >= 2
forThemTextField?.becomeFirstResponder()
else if textField == forThemTextField
if (textField.text?.count)! >= 2
forThemTextField.resignFirstResponder()
【讨论】:
【参考方案3】:将目标添加到 UITextField(在 viewDidLoad() 中),例如:
mTfActuallyFrom?.addTarget(self, action: #selector(CalculatorViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
在委托函数中,使用 UITextField.becomeFirstResponder() 将焦点移动到下一个文本字段。根据您的要求更改您的条件。例如:
func textFieldDidChange(_ textField: UITextField)
if textField == mTfActuallyFrom
if (textField.text?.characters.count)! >= 4
mTfActuallyTo?.becomeFirstResponder()
else if textField == mTfActuallyTo
if (textField.text?.characters.count)! >= 4
dismissKeyboard(gesture: nil)
【讨论】:
我找不到 func textFieldDidChange ! 我更新了我的答案,没有直接委托功能。相反,您必须添加目标。 我在dismissKeyboard(gesture: nil)
中有错误使用未解析的标识符“dismissKeyboard”
@cs4alhaider 将 dismissKeyboard(gesture: nil) 更改为 textField.endEditing(true)以上是关于如何在 Swift 中自动移动到下一个 UITextField的主要内容,如果未能解决你的问题,请参考以下文章
如何在swift ios中自动将光标从一个文本字段移动到另一个文本字段?
如何在swift3 ios中使用键盘返回键将一个文本字段移动到另一个文本字段