I have a UITextField with date picker, and I want to use the default built in button to clear the text field, it doesn't seems to work.
I tried overriding
func textFieldShouldClear(_ textField: UITextField) -> Bool {
searchBar.text = "" //my textfield is called searchBar and I tried to clear the text like that
return true
}
It seems that the method is called and printing the textfield says its empty but its not and in the view shows the last date I entered. Any ideas? Thanks in advance.
You are searching this:
textfield.clearButtonMode
Have you tried to replace searchBar with textField (local variable) in the function like this?
func textFieldShouldClear(_ textField: UITextField) -> Bool {
textField.text = "" // use local variable textfield here
return true
}
I found out that clear button is updating the text field and still takes the date from the date picker... Now I am trying to clear the value of the date picker maybe this will work.
Related
I have a UItextField called textField on a static cell tableView. That field contains a currency amount - like "$10,000.00".
When editing that amount, the currency and thousand grouping symbols are a bit in the way. Therefore I would like to remove those when the field becomes the first responder.
I do this in textFieldShouldBeginEditing.
The first time I do this, everything works. The textField's contents are reformatted without currency and thousand group separator.
On textFieldDidEndEditing I re-format the value into a proper currency string again. This also works.
The problem happens when I re-enter the field for a 2nd time. While debugging I can see the textField.text has changed into the string without currency symbol and grouping symbol, but the display does not show that. Although it did work the first time! The 2nd time it looks like there is a mismatch between what is on the screen and what value the debugger sees.
I've tried things like:
tableView.beginUpdates(); tableView.endUpdates()
textView.setNeedsDisplay
... but this does not work.
So I copied the code that removes the currency formatting in textFieldShouldBeginEditing to a new delegate method textFieldDidBeginEditing.
Then everything worked fine. I could tap other controls and back to the textField a number of times and each time the control would lose its formatting when entered and would be restored to a formatted currency string after losing focus.
So I decided to delete the method textFieldShouldBeginEditing. But then things broke down again! It looks like I have to implement both textFieldShouldBeginEditing as well as textFieldDidBeginEditing to be able to prepare a textField's contents for user-editing?
Is this a bug?
extension Double {
public func doubleToString(numberStyle: NumberFormatter.Style, decimals: Int, withThousandSeparator: Bool) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = numberStyle
numberFormatter.maximumFractionDigits = decimals
if !withThousandSeparator {
numberFormatter.groupingSeparator = ""
}
return numberFormatter.string(from: NSNumber(value: self)) ?? ""
}
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField === self.textField {
textField.text = amount.doubleToString(numberStyle: .decimal, decimals: 2, withThousandSeparator: false)
}
}
You should try textField's text changed event, attaching code below :
Add target on textField for text changed :
self.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Function for textField's text change :
func textFieldDidChange(_ textField: UITextField)
{
if textField === self.textField
{
//try formatting here
}
}
I have a UITextField that holds the text: "Username". I want to erase the user name once the user has selected the text field for editing.
Is there a better way than using the selector method?
Here is the method I'm currently using, but it doesn't seem to be working.
usernameTextField.addTarget(self, action: #selector(selectedUsernameField), for: .editingChanged)
func selectedUsernameField(sender: UITextField){
print("selectedUsernameField")
usernameTextField.text = ""
}
I this case you should set the placeholder for usernameTextField.
usernameTextField.placeholder = "UserName"
It will disappear as user will start typing that's what you want.
You can use this function:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == usernameTextField{
textField.text = ""
}
}
It is called when the text field begins editing.
Although to use it you will need to make your class a UITextFieldDelegate
and also say:
usernameTextField.delegate = self
in viewDidLoad.
How does this code work -
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.resignFirstResponder() {
textField.text = nil
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
theTextField.text = textField.text
}
I don't understand it, I know what it does but i need some help to understand what the codes mean. Thank you! :)
ShouldReturn have to be called earlier than DidEndEditing, if ShouldReturn
ends with true. TextField is a first responder, therefore textField.text and theTextField.text are nil.
I could assume that original idea is to copy value from textField to theTextField, and nullify the first field.
ShouldReturn is called to figure out if editing is finished, and resign method will deactivate currently active textField.
Then after that DidEndEditing(_ textField is going to be called for the first(resigned) textField, but the text is already nil(was reset), that is thy theTextField.text is going to be nil.
Please check the UITextFieldDelegate
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
}