gpt4 book ai didi

ios - 如何以编程方式在 swift ios 中自动将光标从一个文本字段移动到另一个文本字段?

转载 作者:IT王子 更新时间:2023-10-29 05:32:24 24 4
gpt4 key购买 nike

    func textFieldDidBeginEditing(textField: UITextField) {
scrlView.setContentOffset(CGPointMake(0, textField.frame.origin.y-70), animated: true)


if(textField == firstDigit){

textField.becomeFirstResponder()
secondDigit.resignFirstResponder()
}


else if(textField == secondDigit){

textField.becomeFirstResponder()
thirdDigit.resignFirstResponder()
}

else if(textField == thirdDigit){
//textField.becomeFirstResponder()
fourthDigit.becomeFirstResponder()
}

我使用四个文本字段进行一次性密码输入,一次只能输入一个数字。输入数字后,我需要将光标自动移动到下一个文本字段。

最佳答案

设置 textField 委托(delegate)并添加目标:

override func viewDidLoad() {
super.viewDidLoad()

first.delegate = self
second.delegate = self
third.delegate = self
fourth.delegate = self


first.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
second.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
third.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
fourth.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
}

现在当文本更改时更改 textField

func textFieldDidChange(textField: UITextField){

let text = textField.text

if text?.utf16.count >= 1{
switch textField{
case first:
second.becomeFirstResponder()
case second:
third.becomeFirstResponder()
case third:
fourth.becomeFirstResponder()
case fourth:
fourth.resignFirstResponder()
default:
break
}
}else{

}
}

最后,当用户开始编辑明文字段时

extension ViewController: UITextFieldDelegate{
func textFieldDidBeginEditing(textField: UITextField) {
textField.text = ""
}
}

关于ios - 如何以编程方式在 swift ios 中自动将光标从一个文本字段移动到另一个文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35331839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com