UITextfield clear button - iphone

How can I clear the field which currently has the cursor placed inside it?
I have tried:
if(textfield.isEditing)
textfield.text = #"";
This works for me, but if I select all 3 fields, and press the 'clear field button' all the fields clear Also instead of isEditing, I have tried to use tags on the UITextfields and do :
if(textfield.tag == 1)
textfield.text = #"";
but this has the same effect.
How can I solve this problem?

try one or both of these...
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.clearsOnBeginEditing = YES;
Then add delegate...
- (BOOL)textFieldShouldClear:(UITextField *)textField {
return YES;
}

Try out this in TextField's Delegate method didBeginEditing:
if ([textfield isFirstResponder]) {
textfield.text = #"";
}
Hope this helps you.

Related

change return key type of last dynamic generated textfield

I am generating 4 dynamic UITextField all with return key type next and when i press next the focus gets transferred to next UITextField but what i want is when 4th UITextField gets focus its return key type must change to Done which when pressed must resign first responder.
for implementing "next" functionality i am using this code
NSUInteger currentIndex = [Feilds1Array indexOfObject:textField];
if(currentIndex>=Feilds1Array.count-1)
{
NSLog(#"change return key type...");
}
else
{
UITextField* nextTextField1 = (UITextField*)Feilds1Array[currentIndex+1] ;
[nextTextField1 becomeFirstResponder] ;
}
this might be an easy one but i am not able to figure this out as i am a newbie
so please help me out
Thanks in advance...
Do like this,
In your textField delegate method,
access the 4th textfield by using tag value or as you did already,
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if([textField isEqual:textField4])
textField4.returnKeyType = UIReturnKeyDone;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([textField isEqual:textField4])
[textField resignFirstResponder];
return YES;
}
textField.returnKeyType = UIReturnKeyDone;
If your text field is generated by xib in that case you need to set return key of last text field in your xib Done. See in the below image
other wise
NSUInteger currentIndex = [Feilds1Array indexOfObject:textField];
if(currentIndex>=Feilds1Array.count-1)
{
NSLog(#"change return key type...");
UITextField* nextTextField1 = (UITextField*)Feilds1Array[currentIndex+1] ;
nextTextField1.returnKeyType = UIReturnKeyDone;
[nextTextField1 becomeFirstResponder] ;
}
else
{
UITextField* nextTextField1 = (UITextField*)Feilds1Array[currentIndex+1] ;
[nextTextField1 becomeFirstResponder] ;
}
If you are creating UITextField dynamically then you can set the property as
textfieldName.returnKeyType = UIReturnKeyDone;
in your case you can set the above property to the 4th textfield.

UITextView alignment is changing on its own

I have a UITextView with RTL alignment.
If the user enters text, and dismisses keyboard, the alignment remains - RTL.
But if the user chooses empty string, I change the string to the place holder, and the alignment flips to be LTR.
I tried to explicitly ask for RTL, but it didn't help.
-(void)cancelPad{
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
userTextView.text = #"place holder text";
}
-(void)doneWithNumberPad{
if ([userTextView.text isEqualToString:#""]) {
[self cancelPad];
}
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
}
Somebody have any ideas?
Instead of:
userTextView.text = ""
you should put:
userTextView.text = [NSString stringWithFormat:#"\u202b"];
This is the UTF-8 character for "starting RTL"
Eventually I just added a label with the place holder text.
When the keyboard is shown, the placeholderLabel is hidden.
If the keyboard is dismissed with empty text, the placeholderLabel is shown again.
If there is a more proper solution, do not hesitate to post it here.
first:
in the .h file add this
UIViewController<UITextFieldDelegate>
assign your text field to this
userTextView.delegate =self;
then use this methods and put your code in
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
}
Example :
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if ([userTextView.text isEqualToString:#""]) {
[self cancelPad];
}
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
}

simple question about UITextField and not working secureTextEntry

I have a password field with text "Password" and when user clicks, it gets cleared. Moreover I want to set the type of this textfield as secure but this doesn´t work.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([textField.text isEqualToString:#"Password"] || [textField.text isEqualToString:#"Email"])
{
textField.text = #"";
textField.secureTextEntry = YES;
}
}
I've just had the same problem. I was changing the property from within the textFieldDidBeginEditing: call.
Moving the property change to the textFieldShouldBeginEditing: call fixed the problem. I believe the trick is to change it while the text field isn't the becomeFirstResponder.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:_passwordField]) {
textField.secureTextEntry = YES;
}
return YES;
}
Simply put your text in a placeholder and make your password textfield secure. Your problem will be solved :).
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([textField.text isEqualToString:#"Password"] || [textField.text isEqualToString:#"Email"])
{
textField.text = #"";
[textField resignFirstResponder];
textField.secureTextEntry = YES;
[textField becomeFirstResponder];
}
}
it can solve your problem , but it has a big bug. the shit UITextField .
set the property in interface builder or when you initialize textfield remove the line from
-(void)textFieldDidBeginEditing:(UITextField *)textField {
}
I think you should set placeholder text as email or password and pre select the secure text entry from interface builder.That's it...
I realize this is a little old, but in iOS 6 the UITextField "text" is now by default "Attributed" in Interface Builder. Switching this to be "Plain", which is how it was in iOS 5, fixes this problem.
If your keyboard is present, secureTextEntry won't work. Because the UITextInputTraits Protocol doesn't change the textfield if the keyboard shows.
You can dismiss the keyboard, and it will work
[myTextField resignFirstResponder]

set UITextField as non editable - Objective C

[Number.editable = NO];
[Number resignFirstResponder];
[Password.editable = NO];
[Password resignFirstResponder];
I am getting the error
Request for member 'editable' in something not a structure or union
:S
Thanks
Firstly, the [...] aren't needed if you're not sending a message.
Number.editable = NO;
[Number resignFirstResponder];
Password.editable = NO;
[Password resignFirstResponder];
But this is not the cause of error. The .editable property is only defined for UITextView, not UITextField. You should set the .enabled property for a UITextField (note that a UITextField is a UIControl).
Number.enabled = NO;
...
Also, you can use the delegate methods.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
That would do the trick, I prefer this method over setting textField.enabled = YES when it's likely that the ability to edit will change during the lifecycle of the app.
textField.userInteractionEnabled = NO;
Hope this helps..
Returning NO from shouldChangeCharactersInRange would be better choice because if text is longer than the textfield width, then above solution will give problem, because user won't be able to see all text(i.e. text hidden beyond the text field width)

UITextField text value returns garbage

I am trying to get a string value out of a textField when the user dismisses the keyboard. However, it seems that whenever I try to get the value, I get garbage (attempting to print out textField.text gives out garbage). What could I be doing wrong?
(The control displays fine, and I can put text values into it even).
Here's my code:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
NSInteger currenttag = textField.tag;
NSLog(#"%d",textField.tag);
if (currenttag == 0) {
NSLog(#"%x %s",(unsigned int)textField.text,textField.text);
username = textField.text;
} else if (currenttag == 1) {
password = textField.text;
}
[textField resignFirstResponder];
return YES;
}
The fields username and passwords are nil NSString*'s, but since I will merely hold on to the NSStrings held by textField.text, it should be fine.
NSLog(#"text field text:%#",textField.text);
Have you tried using breakpoints? Have you tried NSLog(#"%#", textField.text); ?
Have you tried rewriting the function so it only displays the text?
Is the textField a valid object?
Inserting [textField retain]; as the 1st line will probably fix the problem. Just remember to add a [textField release]; at the end of the method.