You can also do this when the user taps return on the keyboard:
//Add the delegate to the class
UITextFieldDelegate
//In viewDidLoad add the following
//Delegate need to connect return key on keyboard
addField.delegate = self
func textFieldShouldReturn(_ taskFiled: UITextField) -> Bool {
self.view.endEditing(true)
if yourTextField!.isEmpty {
//Empty so close
taskFiled.text = ""
return true
}
//Do whatever the field does here.
return true
}
Hello,
I am trying to add code that will dismiss the numeric keyboard but I am having trouble. I am new to coding so forgive my ignorance. I searched here on the threads and came across this thread. I used the above code but it is not working for me. I am sure it is due to my error somewhere.
I am placing the code in the view controller where my UITextFields are located and at the end of the code. Is that the correct view controller? If so, where in the code should it be placed? Below is the code that I am using.
first try checking if the “touchesBegan” function really is being called. try putting a print statement and print anything just to see if it prints out then you want to hide your keyboard
yes because thats the code that get fired when you touch the screen, hideKeyboard in this case is just a function so basically you can even just do “efficiency.resignFirstResponder()” inside the touchesBegan