How to disable a save button until all the fields are filled in so it prevents the user from continuing until they enter something.
How can I do that in Swift?
First write a method to check for your textfield's text and only enable the button if the text fields contain valid text:
func checkField(sender: AnyObject) {
if textField1.text.isEmpty || textField2.text.isEmpty
{
yourbutton.enabled = false
}
else
{
yourbutton.enabled = true
}
}
Then call your function from the text field's delegate method textFieldDidEndEditingso that when the user finishes editing a field the button is enabled/disabled correctly.
Related
I currently have four text fields. At the beginning, only one text field shows. After the user enters text to the first text field and taps a button a new text field is presented and the previous one is hidden with the text field hidden property. My current code only shows the first text field, hides it and shows a new one, and stays in the second text field. There are still two more text fields that need to have the same functionality.
Can you explain why my current code is not working?
Thank you
This is my current code
if nameTextField.text != nil {
nameTextField.resignFirstResponder()
emailTextField.isHidden = false
nameTextField.isHidden = true
emailTextField.becomeFirstResponder()
} else if emailTextField.text != nil {
emailTextField.resignFirstResponder()
emailTextField.isHidden = true
firstPasswordTextField.isHidden = false
firstPasswordTextField.becomeFirstResponder()
} else if firstPasswordTextField.text != nil {
firstPasswordTextField.resignFirstResponder()
firstPasswordTextField.isHidden = true
phoneNumberTextField.isHidden = false
phoneNumberTextField.becomeFirstResponder()
} else if phoneNumberTextField.text != nil {
phoneNumberTextField.resignFirstResponder()
}
Hi Mathew the condition what you have written is incorrect . Lets say user first enter in nameTextField and then you are checking emailTextField not equal to nil.. Sone ones user enter in both the textfield , it is always going to satisfy the first condition only.. Just put a brteakpoint and check your logic.
When I use default Security text Entry in UITextField in Swift Language after type type text once UITextField.
Once loss focus from UITextField after try to edit Secure text then UITextField is first reset and after it start put new text in UITextField.
How to edit old Secure Text without Storing data into Any Kind of String object
I'd suggest to create a custom UITextField class and override become​First​Responder() method do add your desired functionality:
You can override this method in your custom responders to update your
object's state or perform some action such as highlighting the
selection. If you override this method, you must call super at some
point in your implementation.
The custom Class should be similar to:
class CustomSecureTextField: UITextField {
override func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
if !isSecureTextEntry { return true }
if let currentText = text { insertText(currentText) }
return true
}
}
The logic of the implementation of becomeFirstResponder as follows:
By default, the secured-entry text field clears the inserted text when it is become first responder text, so what's happening in CustomSecureTextField that if the text field is secured-entry, it will re-insert the current inserted text -after clearing it-, but you have to make sure that the text field input is secured (that's the purpose of adding if !isSecureTextEntry { return true }) or the text will be duplicated (re-inserted) each time the text field becomes first responder.
Output:
Note that both of text fields are types of CustomSecureTextField:
This answer helped me to figure out this problem.
textField.isSecureTextEntry = true
following property not gonna work if you make testField isSecureTextEntrysecure property makes true .
textField.clearsOnBeginEditing = false
There is an issue with the #Ahmd F solution when you simply tap on the field it will automatically add the text to the field I have resolved that in the below code thanks
override open func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
if !isSecureTextEntry { return true}
if let currrentText = text {
self.text = ""
insertText(currrentText)
}
return true
}
I'm trying to hide the text of a UITextView, used as a password entry, using dots instead of the actual text. This text view is called TV_Password.
When its empty, the text should not be hidden and should be replaced by the string "Password"
I found on the net that the solution would be to change the following property to true.
self.TV_Password.secureTextEntry = true
Unfortunately, this still doesn't hide my text.
I moved the modifications to textViewShouldBeginEditing instead of textViewDidBeginEditing, as advised to people having this kind of issue, but it still doesn't work.
I haves various breakpoints telling me the instruction IS really done, but nothing happens..
Any idea .?
//MARK: TextViews editing
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
if (textView == self.TV_Password){
if (self.TV_Password.text == "Password"){
//empty text
self.TV_Password.text = ""
//enable secured text
self.TV_Password.secureTextEntry = true
}
}
return true
}
func textViewDidEndEditing(textView: UITextView) {
if (textView == self.TV_Password) {
//if password empty
if (self.TV_Password.text == ""){
//disable secured text
self.TV_Password.secureTextEntry = false
//fill with the word "Password"
self.TV_Password.text = "Password"
}
}
}
ANSWER :
UITextView doesn't have a secure entry mode, use a UITextField instead.
Thanks a lot!
I am trying to understand delegate methods in general, and specifically how to dismiss a UIDatePicker that popovers from a text field.
According to the documentation, textFeildShouldBeginEditing returns true 'if an editing session should be initiated; otherwise, false to disallow editing.'
Why would I then tell the app to resignFirstResponder, which is meant to hide the keyboard / date picker (as in several examples on stackoverflow and noobie tutorials)?
What I don't understand is: if it should begin editing, why then hide the input devise? Obviously, I am misunderstanding one or both concepts.
func resign() {
dobTextField.resignFirstResponder()
nameTextField.resignFirstResponder()
println("resign gets printed, but the date picker is still visible!?!")
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if (textField === dobTextField) {
resign() // but should begin editing, oder?!?
}
In the examples you cite, the textField is being used to display a date. When the user selects this field, the app designers want the UIDatePicker to be displayed instead of the keyboard. Hence they call resignFirstResponder to hide the keyboard. At the same time, they display the date picker.
ResignFirstResponder will not hide the date picker, so the "input device" (for this field) will still be available.
Also, note that in one case the developer has used textFieldShouldBeginEditing, and returns false because they are providing the date picker. In the other case the developer uses textFieldDidBeginEditing (which has no return value).
you should resign only the textfield not affected:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == dobTextField {
nameTextField.resignFirstResponder()
} else if textField == nameTextField {
dobTextField.resignFirstResponder()
}
return true
}
this way you are resiging first responder only on the textfields that should not currently be edited. this helps if for some reason you accidentally have 2 textfields (or more) assigned first responder causing conflicts with multiple keyboards/datepickers and such.
I have a password textfield with a default value "password". The field is marked as secure. I'm trying to erase the default value once the user touch up inside the textfield.
- (IBAction)PasswordTouchUpInside:(id)sender
{
if (Password.text == #"Password")
{
Password.text = #"";
}
}
Is the touch up inside the wrong event?
You should compare strings using the method isEqualToString in the if condition:
if ([Password.text isEqualToString:#"Password"]) {
Password.text = #"";
}
Or, you can simply use the placeHolder property so that the UITextField does this behaviour automatically without you having to write PasswordTouchUpInside.
You probably want Touch Down, not up.