下面提到的是我的textField委托方法,我将IQKeyBoardSwift用于智能键盘。
我尝试卸下键盘,但仍然无法通过任何接受“开始触摸”的方法来接听电话
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
}
return true
}
// Resign first reponder on touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: view)
if !textField1.frame.contains(location) {
textField1.resignFirstResponder()
}
if !textFIeld2.frame.contains(location) {
textFIeld2.resignFirstResponder()
}
if !textField3.frame.contains(location) {
textField3.resignFirstResponder()
}
if !textFIeld4.frame.contains(location) {
textFIeld4.resignFirstResponder()
}
self.textField1.endEditing(true)
self.textFIeld2.endEditing(true)
self.textField3.endEditing(true)
self.textFIeld4.endEditing(true)
}
//MARK: UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("textFieldShouldBeginEditing")
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
print("textFieldDidBeginEditing")
print("Leaving textFieldDidBeginEditing")
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
if(textField == textField1)
{
if(newString.characters.count) == 1 {
print(textField1)
textFIeld2.becomeFirstResponder()
return false
}
}
if(textField == textFIeld2)
{
if(newString.characters.count) == 1{
textField3.becomeFirstResponder()
return false
}
}
if(textField == textField3)
{
if(newString.characters.count) == 1{
textFIeld4.becomeFirstResponder()
return false
}
}
if(textField == textFIeld4)
{
if(newString.characters.count) == 1{
buttonOutletSubmitCode.isHidden = false
resignFirstResponder()
return false
}
}
return true
}
我希望我的光标在收到1个字符的输入后跳到下一个文本字段。不幸的是,这没有发生。
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self as? UITextFieldDelegate
textFIeld2.delegate = self as? UITextFieldDelegate
textField3.delegate = self as? UITextFieldDelegate
textFIeld4.delegate = self as? UITextFieldDelegate
}
这是我的viewDidLoad
最佳答案
添加:
添加协议UITextFieldDelegate
并尝试替换textField1?.delegate = self as UITextFieldDelegate
替换
这个不起作用:func textFieldDidBeginEditing(textField: UITextField) {
}
这适用于:func textFieldDidBeginEditing(_textField: UITextField) {
}
用于跳转文本字段
看到:https://stackoverflow.com/a/18711677/3901620
关于ios - 未调用textField委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43714883/