erase textfield value on touch up inside - ios

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.

Related

How can I resign from one text field to the next while hiding the previous text field?

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.

How to unclear UITextField secure text entry in Swift?

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
}

secureTextEntry doesn't hide the text of my UITextView

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!

Disable button until fields are entered in Swift

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.

dynamically change UIKeyboards return key

I have two UITextfield the user enters his name into the first and email into the second. I would like to know how to change the UIKeyboards return key depending if the name text field has an entry or not.
For instance if nametextfield is empty then I would like the UIkeyboard return key to be Next
else if the nametextfield has an entry in it then when the user selects the email text field I would like the return key to be submit.
Is this possible? if so how would I go about accomplishing it? any help would be appreciated.
You can have return key customized to prefixed values that you can see in UIReturnKeyType enum for each UITextField.
textFieldName.returnKeyType = UIReturnKeyNext;
textFieldEmail.returnKeyType = UIReturnKeyDefault;
Not sure if this is what you're looking for though.
You have a chance to set up keyboard characteristics in the UITextFieldDelegate Protocol method textFieldShouldBeginEditing: which is called before the text field becomes the first responder (indeed to decide if it may become the first responder). If you don't already have a delegate for the text field(s) in question you would have to assign one and implement at least that method. Presumably the same object handling the text field could hold the delegate methods. The following implementation sets the return key to "Search".
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
textField.returnKeyType = UIReturnKeySearch;
return YES;
}
You'd have to look at the contents of your text fields to decide what value to use.
Make use of the textField.returnKeyType property.
you can check out all the available options here http://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/c_ref/UIReturnKeyType
textfield.returnKeyType = UIReturnKeySearch;

Resources