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
}
Related
I am using swift in a storyboad application, and I would like it to, when a user enters the box (IE, when it becomes the first responder) to enter text it highlights all the text. What is done in most web browsers. Here is what I have tried, which has not worked:
func textFieldDidBeginEditing(_ textField: UITextField) {
// textField.selectAll(nil)
print("click 2")
// textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("click 1")
// textField.selectAll(nil)
// textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
return true
}
Making a button that runs this code does actually work, but I want it to be when you click on the field. Making an invisible button on top seems like a bad idea. I've tried other variations of this as well, but none have worked for me.
UPDATE: What I've ended up doing was making an invisible button on top of the existing text field, and then making that select the text. It may be a little janky but it works perfectly so I have no problems with that solution anymore. Extra code:
#IBAction func seachBtnClicked(_ sender: Any) {
//The invisible button over the search bar has been clicked
search.selectAll(nil)
searchBtn.isHidden = true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//Return has been pressed, so we are going to load a new page
processInput()
//This does mean that it only comes back if you press enter,
//but right now I think that's how I want it.
//Could change it to be in the textFieldDidEndEditing() function to change that
searchBtn.isHidden = false
return true
}
You are probably just missing the textField.delegate = self for your textfield. The first of the two functions, func textFieldDidBeginEditing(_ textField: UITextField) is the one that is always performed as soon as a textfield is tapped on to focus on it.
Try adding yourTextField.delegate = self in your viewDidLoad function and see if that works out.
What I've ended up doing was making an invisible button on top of the existing text field, and then making that select the text. It may be a little janky but it works perfectly so I have no problems with that solution anymore. Extra code:
#IBAction func seachBtnClicked(_ sender: Any) {
//The invisible button over the search bar has been clicked
search.selectAll(nil)
searchBtn.isHidden = true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//Return has been pressed, so we are going to load a new page
processInput()
//This does mean that it only comes back if you press enter,
//but right now I think that's how I want it.
//Could change it to be in the textFieldDidEndEditing() function to change that
searchBtn.isHidden = false
return true
}
New to Swift and making a simple to-do app.
I am trying to get it so that when a UI TextField is clicked on, a certain button should be hidden. It's only when the user presses enter after typing in a task that the button should appear.
I have an IBAction set up for my text field to hide the UIButton when it is clicked on like so, but it doesn't work:
#IBAction func textFieldClicked(_ sender: Any) {
self.cellButton.isHidden = true
}
And I have set up my textfieldshouldreturn function when the user presses enter like so:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.cellButton.isHidden = false
self.cellButton.isUserInteractionEnabled = true
textField.resignFirstResponder()
return true
}
Does an IBAction for text field only respond when enter is pressed? I tried messing with flags but that didn't work either.
Any help is appreciated.
You don't need an IBAction for this. Instead, implement another UITextFieldDelegate method - textFieldDidBeginEditing.
func textFieldDidBeginEditing(_ textField: UITextField) {
self.cellButton.isHidden = true
}
This delegate is called when a text field becomes the first responder.
On a slightly unrelated note, it's best to return false instead of true from your textFieldShouldReturn method.
Please, use delegates method textFieldDidDeginEditing and hide button.
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
}
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 :)
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.