textField委托没有被调用

时间:2017-05-01 05:51:21

标签: ios xcode swift3 uitextfield uitextfielddelegate

下面提到的是我的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

2 个答案:

答案 0 :(得分:1)

添加:

添加协议UITextFieldDelegate并尝试替换textField1?.delegate = self as UITextFieldDelegate

替换

没有工作:

func textFieldDidBeginEditing(textField: UITextField) {
}

此作品

func textFieldDidBeginEditing(_textField: UITextField) {
}

用于跳转textField

请参阅:https://stackoverflow.com/a/18711677/3901620

答案 1 :(得分:0)

添加textFiled委托:

class ViewController: UIViewController  , UITextFieldDelegate{

是的,因为你的委托是OLD swift版本而不是Call。将其替换为Swift 3代码。

func textFieldShouldBeginEditing(_ 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
}