UITextField resignFirstResponder not working nor working self.view.endEditing ()

I am facing a ridiculous problem for textField, I have two text fields, namely tfA and tfB, I set delegates and everything for these text fields, what I wanted, if I click on tfA, then it should print something, and it’s printing and if I press tfB, the keyboard should appear, well, it works well too, but when I press tfA again, it should print something, and the keyboard should deviate in accordance with the given condition, but the keyboard there doesn’t turn off either self.view.endEditing(true)does not work here. The code is below with a screenshot, what am I doing wrong here?

CODE: Swift 3

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var tfA: UITextField!
    @IBOutlet weak var tfB: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        tfA.delegate = self
        tfB.delegate = self
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == tfA{
            print("TFA Clicked")
            textField.resignFirstResponder()
            self.view.endEditing(true)
        }else{
            tfB.becomeFirstResponder()
        }
    }

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

        return true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Screenshots

enter image description here

+7
4

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
   if textField == tfA
   {
       tfaAction()
       return false
   }
   else
   {
       return true
   }
}
func tfaAction(){

 }
+17

textFieldDidBeginEditing, textFieldShouldBeginEditing:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  if textField == tfA{
    print("TFA Clicked")
    self.view.endEditing(true)
    return false
  }else{
    return true
  }
}
+6

Swift 4 Just check, please, delegateif this one exists, then it shouldreturn true

'func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {'


    //MARK:- Textfield Delegates //This is mandatory 
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }

    //MARK:- This Delegate is option  but if this is exist in your code then return type shoud be true 
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {

        return true
    }
0
source

run textfield.resignFirstResponder()

so everything will be like

textfield.resignFirstResponder()
self.view.endEditing(true)
0
source

Source: https://habr.com/ru/post/1674097/


All Articles