Hide Keyboard IOS Swift - ios

I happening the following problem:
I have two TextField, one has the default behavior, and on the other, captures the textFieldDidBeginEditing method delegate, to present a UIViewController with PresentationStyle (Custom) and TransitionStyle (CrossDissolve). So far everything works fine. But if I edit the first textField (and leave the keyboard open) and then go to the second, the keyboard is open and I have no way to close it (as if the first textField had lost focus, not even the textFieldShouldReturn is called when I press the button intro).
I tried with:
TextFieldDidEndEditing (for calling resignFirstResponder)
In viewWillDissaper method (I also called resignFirstResponder)
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
print("Resign Last TextField")
textField.resignFirstResponder()
}
func textFieldDidBeginEditing(textField: UITextField) {
print("Did Begin editing")
if textField == self.departing || textField == self.returning{
textField.resignFirstResponder()
self.lastTextFieldSelected = textField
let datePickerViewController = Util.getViewController("DatePickerViewController") as! DatePickerViewController
datePickerViewController.dateStyle = NSDateFormatterStyle.ShortStyle
datePickerViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
datePickerViewController.modalPresentationStyle = UIModalPresentationStyle.Custom
datePickerViewController.datePickerDelegate = self
self.presentViewController(datePickerViewController, animated: true, completion: nil)
}
}
Edit:
I try with self.view.endEditing(true) nothing.
I made a little example (something I check, is that textFieldShouldBeginEditing allows me to close the keyboard.):
https://github.com/Abreu0101/TextFieldBug

It looks like your textFieldDidEndEditing and the other methods are not even called. Make sure you set the UITextField's delegate:
textField.delegate = self
Also import the UITextFielDelegate in your class.
Hope that helps :)

Related

Hide keyboard when a textField is clicked

I want to hide the keyboard every time a user click on the textField named typeDeProbleme. So, I did something like below (in the if condition) which in my understanding should've work but unfortunately it's not. Every time I click on the typeDeProbleme, the keyboard is stuck and cannot be hidden unless I click on another textField. For your information, typeDeProbleme is a textField linked to a PickerView. I also attached a toolbar on top of my keyboard that have a button X to hide the keyboard. Can anyone explain to me where is the problem?
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.returnKeyType = .next
if textField == self.typeDeProbleme {
textField.endEditing(true)// hide keyboard
textField.resignFirstResponder() //hide keyboard
self.pbTypePickerV.isHidden = false
}else{
keyboardDidShow(vvv: textField)
}
}
Use this method instead textFieldDidBeginEditing
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == typeDeProbleme {
self.view.endEditing(true)
return false
}
return true
}

textFieldShouldReturn not working

I have two classes. In one of them, the return key (for the virtual keyboard) works perfectly. In the other, it does not. As far as I can tell, the code is pretty much identical. Can someone help me figure out why my its not firing on the one that doesn't work?
This one works:
class ChangePasswordVC: UIViewController, UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.tempPasswordTextField {
self.changeTempPasswordTextField.becomeFirstResponder()
}
else if textField == self.changeTempPasswordTextField {
self.confirmChangeTempPasswordTextField.becomeFirstResponder()
}
else {
changePasssword()
}
return true
}
}
This one does not work:
class ResetPasswordVC: UIViewController, UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.emailTextField {
print("go button tapped")
submitEmailForPasswordReset()
}
return true
}
}
I put the print statement in to see if it fires at all but it doesn't print anything to the console.
You have forgot probably some of the delegate settings, check for:
self.tempPasswordTextField.delegate = self
self.changeTempPasswordTextField.delegate = self
self.confirmChangeTempPasswordTextField.delegate = self
self.emailTextField.delegate = self
In your code especially check the last (self.emailTextField).
You have to set UITextField delegate either using StoryBoard/XIB or using code. It will work fine when UITextField delegate assigned correctly.

UITextField - delegate for clearButtonMode?

I want the X button to show only when
textField.clearButtonMode = .UnlessEditing
When the user pushes the X button, the textView becomes the first responder, and the keyboard pops up.
What do I do so that when the X button is pushed, the text only clears, but the textView does not become focused?
- (BOOL)textFieldShouldClear:(UITextField *)textField
this method is your want,you can check something in it.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006991-CH3-SW10
i hope above link can help you.
If you want it to be focus and able to key in, try
textField.becomeFirstResponder()
If you mean you want it not to show the keyboard and just clear the text, you can delegate your UITextField and use the function to return false:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
textField.text = ""
return false
}
You can implement this delegate like the following:
func textFieldShouldClear(_ textField: UITextField) -> Bool {
textField.text = ""
return false
}

Swift - change action event to UITextField

i'm trying to change the default action when user press on a text field. I tried to connect my action at Editing Did Begin event using storyboard as you can see below. The problem is that the keyboard always appear but I want that the keyboard doesn't never appear.
I cannot delete text field delegate methods cause i have others text field in the same view.
How can i do?
Set your view controller as your textField delegate and implement UITextField textFieldShouldBeginEditing delegate method.
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == yourTextField {
// implement custom behaviour
return false
}
return true
}
Update
Try this for single textfield keyboard never show
func textFieldDidBeginEditing(textField: UITextField) {
if textField == yourTextField
{
textField.inputView = UIView()
}
}
Simple:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if(textField == YourtextField){
return false
}
return true
}
OR:
in your viewDidLoad() set yourTextField.inputView = UIView()
Works in both ways. Use suitable one.

resignFirstResponder and endEditing (even global calls) are not hiding the keyboard

I have a ViewController set as a delegate for a UITextField. No matter what I try, I can not get the keyboard to hide when calling textFieldShouldReturn.
My viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(newModelField)
newModelField.delegate = self
self.presetTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "presetCell")
//self.view.becomeFirstResponder()
}
and my textFieldShouldReturn method:
func textFieldShouldReturn(textField: UITextField) -> Bool {
//UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
if(textField.text! != ""){
items.append(textField.text!)
presetTableView.reloadData()
textField.text! = ""
//self.view.resignFirstResponder()
//self.addView.resignFirstResponder()
//textField.endEditing(true)
//self.view.endEditing(true)
UIApplication.sharedApplication().delegate?.window?!.endEditing(true)
return false
}
return true
}
You can see from my commented out attempts that I have tried a whole host of different fixes. I know that textFieldShouldReturn is being called after debugging with print statements. I can not figure out why the keyboard won't hide.
I tried swapping the return true and false statements. I tried setting the delegate from the storyboard. I tried deleting the textField from storyboard, cleaning, and adding it back in. I tried all top level calls to resignFirstResponder and endEditing as well as calls to these functions for the textfield and the view.
To dismiss the keyboard, send the resignFirstResponder message to the text >field that is currently the first responder. Doing so causes the text >field object to end the current editing session (with the delegate >object’s consent) and hide the keyboard.
yourTextField.resignFirstResponder()
I tried using resignFirstResponder() on textFiled and also used endEditing(true) on my view but I did this small mistake of implementing this other delegate and was returning false
func textFieldShouldEndEditing(textField: UITextField) -> Bool { //delegate method
return true
}

Resources