gpt4 book ai didi

ios - 按下键盘上的键后如何验证文本字段

转载 作者:行者123 更新时间:2023-11-29 00:09:55 28 4
gpt4 key购买 nike

我有 3 个文本字段

txtField1
txtField2
txtField3

用户需要使用键盘上的 Key(Next) 转到下一个文本字段

要求:

txtField1 键盘上将有 (Next)-键
txtField2 键盘上将有 (Next) 键
txtField3 键盘上将有 (go)-键

问题:
如何按键盘上的下一个键转到下一个 txtField 并按键盘上的 (Go) 键进行验证,并检查 txtField 是否为空。

My Code :

1. The class conform to UITextFieldDelegate

Class myTestVC : UIViewController, UITextFieldDelegate {}

override fun viewDidLoad() {

txtField1.delegate = self
txtField1.tag = 0
txtField1.returnKeyType = .next
txtField1.borderStyle = UITextBorderStyle.none

txtField2.delegate = self
txtField2.tag = 1
txtField2.returnKeyType = .next
txtField2.borderStyle = UITextBorderStyle.none

txtField3.delegate = self
txtField3.tag = 2
txtField3.returnKeyType = .next
txtField3.borderStyle = UITextBorderStyle.none

//- to show the Keyboard
txtField1.becomeFirstResponder()


}

2) How to implement this for the above scenario?

func textFieldShouldReturn( _ textField: UITextField) -> Bool {


}

请帮忙。

谢谢

最佳答案

你可以这样使用:

override func viewDidLoad() {
super.viewDidLoad()
txtField1.delegate = self
txtField1.tag = 0
txtField1.returnKeyType = .next
txtField1.borderStyle = UITextBorderStyle.none

txtField2.delegate = self
txtField2.tag = 1
txtField2.returnKeyType = .next
txtField2.borderStyle = UITextBorderStyle.none

txtField3.delegate = self
txtField3.tag = 2
txtField3.returnKeyType = .go
txtField3.borderStyle = UITextBorderStyle.none

//- to show the Keyboard
txtField1.becomeFirstResponder()
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
switch textField
{
case txtField1:
txtField2.becomeFirstResponder()
case txtField2:
txtField3.becomeFirstResponder()
default:
if textField.returnKeyType == .go {
var txtError = ""
if(txtField1.text == nil || txtField1.text!.isEmpty) {
txtError = "txtField1"
}
if(txtField2.text == nil || txtField2.text!.isEmpty) {
txtError += "txtField2"
}
if(txtField3.text == nil || txtField3.text!.isEmpty) {
txtError += "txtField3"
}

if txtError.isEmpty {
textField.resignFirstResponder()
} else {
let alert = UIAlertController(title: "Alert", message: "\(txtError) are empty", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
return true
}

关于ios - 按下键盘上的键后如何验证文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46782254/

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