1

Below mentioned are my textField delegate methods, I am using IQKeyBoardSwift for inteligent keyboard. I tried removing my keyboard but still I am not receiving call on any of my method accept 'begins touch'

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
}

I want my cursor to jump to next textfield after receiving an input of 1 character. Unfortunately That is not happening.

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
    }

This is my viewDidLoad

2 Answers2

1

Adding :

Add protocol UITextFieldDelegate and try to replace textField1?.delegate = self as UITextFieldDelegate

Replace

This doesn't work:

func textFieldDidBeginEditing(textField: UITextField) {
}

This works:

func textFieldDidBeginEditing(_textField: UITextField) {
}

For jump textField

see : https://stackoverflow.com/a/18711677/3901620

Community
  • 1
  • 1
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Adding textFiled delegate :

class ViewController: UIViewController  , UITextFieldDelegate{

Yes, Its Not Call because of your delegate is OLD swift version. Replace it Swift 3 code.

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
}
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112