This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detect backspace in UITextField
I am trying to detect the backspace key envent on my UITextfield which is empty. I saw this but could not figure it out. Any elaborate answer or code snippet would be helpful
Detect backspace in UITextField
I've found it not to be possible.
When the UITextField is empty, the following delegate method isn't called.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
I conquered this by constantly having a space in the field, and when I detect a backspace and the field only contains a space, I return NO in the delegate method.
if ([string isEqualToString:#""] && [textField.text isEqualToString:#" "]){
// Backspace called on 'empty' field.
return NO;
}
Visually, this is as good as the field being empty, and it's a workaround to the delegate method not being called on an empty field.
Hope that helps.
:) just for the title "Detect backspace", where I use UIKeyboardTypeNumberPad.
I also meet the same question tonight, and following is my code to find it out:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog([NSString stringWithFormat:#"%d", [string length]]);
}
Because with UIKeyboardTypeNumberPad, user can only input Number or backspace, so when the length of string is 0, it must be backspace key.
Hope the above will do some help.
Related
In my app I have a textfield in which users have to enter a string from 1-25 characters. The problem I'm having is stopping them from only entering spaces. I want them to be able to use spaces but only if they have other characters (abc, 1-9, etc.). How can I stop them from only entering spaces?
In your view controller set delegate of UITextField to self. Then implement following method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if([[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0){
return NO;
}
return YES;
}
Sounds like this question might help.
Just trim the whitespace off the string and then check to see if it is the empty string.
Perform a whitespace "trim" on their input and check to see if the result matches an empty string.
I want to set UILabel according to the UITextField as it is typed. I mean if user want to type SAMPLE and he starts typing S then the lable should be set as S, then he types A label should also be A and so on. How to achieve this?
Please share suggestions.
Thanks in advance
Simplest way to do is to make a method and connect it with UiTextfield with event UIControlEventEditingChanged which will give you the trace on every character entered in the textfield.
[self.selectedTextField addTarget:self action:#selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];
-(void)enterInLabel
{
selectedLabel.text=selectedTextField.text;
}
The delegate also works for this as #brain said. The shouldChangeCharactersInRange: method can be a little confusing but the the following I think is pretty straight forward.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// myTextField delegate has been set
if ([textField isEqual:myTextField]){
NSMutableString *txt = [NSMutableString stringWithString:textField.text];
[txt replaceCharactersInRange:range withString:string]; //this is essentially how the textfield is updated after YES is returned
previewLabel.text = txt;
}
return YES;
}
The changing of the text field is actually done after the return YES. That is the whole point of this delegate. Just for an example, if you wanted to limit a textfield to 3 characters you could do the following to stop the text field from "replacing" the text.
if (range.location > 3)
return NO;
Set your textField delegate then call its method in your viewController.m file.. like this -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
mylabel.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
This will change the text after each increment of character.
OR You can add action on your TextField
See this link - UiTextField events
Your view controller will need to implement the UITextFieldDelegate which will allow it to receive changes to the text field. In the appropriate methods of the delegate you need to set the text of the UILabel.
I have never used UITextFieldDelegate so can't provide more detail on how to use it. I would mock up a quick example and just NSLog or debug the delegate calls to see that calls you get.
I am making an application in which i want to enter only numeric value. So i choose number pad type keyboard. Now i want that when user click on text filed and enter numeric value then enter only 0,1,2 and 3 not anything else. What i will do for that so i can apply restriction on text-field's value? And enter only single value not double value ie 11.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
}
Thanks in advances...
If rhe values are that restricted, you should try using a picker view or slider as the input view of the field.
How are you going to communicate to the user that 60% of the buttons on the numeric keypad are visible, enabled, tappable, but will do nothing?
in textField:shouldChangeCharactersInRange:replacementString: You can then check the character the user just entered for validity, returning NO for any invalid characters.
Here is a code sample to limit the size of a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[textField text] length] + [string length] - range.length > MAX_LENGTH) {
return NO;
} else {
return YES;
}
}
also see this
I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.
Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
So that could solve your problem.
Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}
Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.
you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...
You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.
I'm writing application in which I need to validate text entered by user to UITextField, char by char with some method.
The difficult thing is that client wants to do all the validation before user see character in the UITextField because there might be situation that his server application doesn't support '$' sign, so in my validation method I should replace it with 'USD' string - and he doesn't want user to see '$', just 'USD' immediately.
I know about events like UIControlEventEditingChanged etc., but still, I don't know 2 things:
how to access character typed by user before it's seen in UITextField and execute validation there
how to subtitute this character 'on the fly' and put it manually to UITextField (but I suppose I'll just append this to [[textField] text] NSString
Thank You in advance for any help :)
Implement the UITextFieldDelegate method textField:shouldChangeCharactersInRange:replacementString:, e.g.:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
BOOL validated = ...; //do your validation
return validated;
}
Similar to omz's answer but more complete code if you need:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *filtered;
filtered = [string stringByReplacingOccurrencesOfString:#"$" withString:#"USD"];
//optionally if you want to only have alphanumeric characters
//NSMutableCharacterSet *mcs1 = [[[NSCharacterSet letterCharacterSet] invertedSet] mutableCopy]; //only alphabet character
//[mcs1 removeCharactersInString:#"0123456789"];
//filtered = [[filtered componentsSeparatedByCharactersInSet:mcs1] componentsJoinedByString:#""];
//release mcs1;
return [string isEqualToString:filtered];
}