9

I'm facing a ridiculous problem for textField, I have two textfield, namely tfA and tfB, I have set delegates and all for those textfields, what i wanted is, if I click on tfA then it should print something, and it is printing and if I click tfB it should appear the keyboard, well it is working well too, but when I click again to tfA then it should print something and keyboard should dismiss according to the condition given there, But keyboard is not dismissing there also self.view.endEditing(true) is not working here . Code is given below with screen shot, what I'm 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

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46

4 Answers4

20

Try this

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

 }
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • Thanks for your effort, but it is not working either, my problem is if you click on tfB keyboard will be shown and if you click on tfA keyboard should be dismissed. Also if you click on tfA first then keyboard shouldn't be shown. but in this I need an action on clicking tfA. – Abhishek Mitra Apr 05 '17 at 14:55
  • Thank Hero @RajeshKumar R, worked for me in just 5 Seconds. – Ravi Aug 22 '18 at 09:49
7

Remove your textFieldDidBeginEditing method, replace it with textFieldShouldBeginEditing:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  if textField == tfA{
    print("TFA Clicked")
    self.view.endEditing(true)
    return false
  }else{
    return true
  }
}
Craig Grummitt
  • 2,945
  • 1
  • 22
  • 34
0

Swift 4 Only check please this delegate if its exist then its should return 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
    }
Mike
  • 4,041
  • 6
  • 20
  • 37
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
-1

run textfield.resignFirstResponder()

so everything will be like

textfield.resignFirstResponder()
self.view.endEditing(true)
sese smith
  • 356
  • 3
  • 5