Calling a method on every edit of UITextField - ios

I'm now programming a simple app that has 3 UITextFields and if I edit one, the other two should scale together with it.
I tried using
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
this method, but:
replacement string is the last character that was typed in
can't figure out how backspace works there
it is being called a little too early
if I can "fix" the first point(by sending
[NSString stringWithFormat:#"%#%#", [textField text], string];
as a parameter), it will not "fix" the second point, because string variable is:
(lldb) po string
(NSString *) $1 = 0x0080cf14 <object returned empty description>
So the question is: is there any method that is being called AFTER textFieldShouldChangeCharactersInRange:? Or is there a way to:
return YES in textFieldShouldChangeCharactersInRange: method
and THEN call a method to change the values of the 2 other UITextFields?
EDIT
I could use the following:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[self performSelector:#selector(myMethod:) withObject:textField afterDelay:0.1];
return YES;
}
but it doesn't seem to be the safest solution

The backspace works modifying the NSRange with a empty string. What you can do is modify the three text field in the textField:shouldChange and then return NO to the method.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; // this is what will happen if you return yes to this method
anotherTextField.text = // do whatever u need
yetAnotherTextField.text = // do whatever u need
return NO;
}

The following code prints correctly the full text of the text view (with the last character that was inserted), can that be useful?
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string length])
NSLog(#"Current Text:%#", [textField.text stringByAppendingString:string]);
else
NSLog(#"Current Text:%#", [textField.text substringWithRange:(NSRange){0, [textField.text length]-1}]);
return YES;
}
The backspace just sends an empty string in the string parameter.

For textFields:
[yourtextField addTarget:self action:#selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
-(void)textFieldDidChange
{
//Your Code
}

Related

Local declaration of 'fieldPhone' hides instance variable

I keep getting the warning that I posted in the title with these methods...I put self. in front of them but it then messed up my menus. I saw some similar posts, but nothing to the point and absolute. Any help on this is appreciated.
- (BOOL)textField:(UITextField *)fieldPhone shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* totalString = [NSString stringWithFormat:#"%#%#",self.fieldPhone.text,string];
// if it's the phone number textfield format it.
if(fieldPhone.tag==102 ) {
if (range.length == 1) {
// Delete button was hit.. so tell the method to delete the last char.
fieldPhone.text = [self formatPhoneNumber:totalString deleteLastChar:YES];
} else {
fieldPhone.text = [self formatPhoneNumber:totalString deleteLastChar:NO ];
}
return false;
}
return YES;
}
Change the function textField name to a generic name, problem is that you named it equal to your class variable, change it from:
- (BOOL)textField:(UITextField *)fieldPhone shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
to:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
You can check it inside the function if needed:
if (textField == self.phoneField) {
}

Restrict user to enter abuse words in text view

I am developing a application where user has to enter only holy words. I want user to be restricted not to enter the abuse or adult word.
I have a big list of adult or abuse words whenever user will enter that word it should delete it automatically.
Any help will be appreciated.
You are probably using UITextField so you should look after forbidden words after text has changed:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSRange spaceRange = [newString rangeOfString:#" "];
if (spaceRange.location != NSNotFound) { // it's a new word
newString = [self stringWithoutForbiddenWords:newString];
}
textField.text = newString;
return NO; // we set the textField text manually
}
- (NSString *)stringWithoutForbiddenWords:(NSString *)string {
for (NSString *forbiddenWord in self.forbiddenWords) {
NSRange forbiddenWordRange = [string rangeOfString:forbiddenWord];
if (forbiddenWordRange.location != NSNotFound) {
// remove the forbidden word
string = [string stringByReplacingOccurrencesOfString:forbiddenWord withString:#""];
}
}
return string;
}
Don't forget to set you UITextField delegate.
Its a very simple logic , by the way only "Holy Word" seems very funny I hope you meant non-abusive words.
So to restrict abusive words, first make an Array and store all the abusive words in that.
then in textView shouldChangeTextInRange: check whenever user press " space.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#" "])
{
//now iterate the whole string and find whether any word contains any value from your Abusive words Array and replace the word with blank space or *
}

Check the content of NSString with data in NSArray

I have the next problem:
In a UITextField, I need to check if the user inserts an invalid character for the Name, like [ ] { }.
My question is, how can I check the content of the UITextField with one NSArray with the invalid characters inside?
If you want to check single letter input from keyboard on some input politics
you need to use textField delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
You should check if string contains in your exception array. If so you return NO and symbol doesn't appear on screen.
Ok, I explain what I do to solve the problem. Cause I'm controlling the behavior of a UITextField where I use to the user enter the name and one comment, I make a character set with all characters I allow and I put inside the shouldChangeCharactersInRange Method:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:#" ,.abcdefghijklmnopqrstuvwxyzçñ?!'ABCDEFGHIJKNMLOPQRSTUVWXYZÇÑ-_;:0123456789+*()-/#áàéèíìóòúù"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:#""];
return [string isEqualToString:filtered];
}

using rangeOfString and textfield shouldChangeCharactersInRange - the delete character?

I'm using rangeofString and textfield: shouldChangeCharactersinRange: to restrict the types of keystrokes that will be valid in a textfield.
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *includeString = #"1234567890-()+" ;
if ([includeString rangeOfString:string].location == NSNotFound) {
return NO;
}
return YES;
}
this works fine EXCEPT i now can't use the delete key. Any ideas how to represent the delete key to add it to the includeString?
I tried
`NSString *includeString = #"1234567890-()+\b"
but that didn't work - neither did it allow the \ or b characters to appear which i thought odd
Thanks
The replacement string string is empty when characters are deleted.
Since rangeOfString:string returns NSNotFound for an empty string,
you have to check for that situation first:
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string length] == 0)
return YES;
NSString *includeString = #"1234567890-()+" ;
if ([includeString rangeOfString:string].location == NSNotFound) {
return NO;
}
return YES;
}
Update: As #rmaddy correctly pointed out, the above method fails if more than one
character is pasted into the text field. The following method checks if all
characters of the replacement string are valid. (There are probably many solutions,
this is only one of them.)
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
static NSString *includeString = #"1234567890-()+";
NSCharacterSet *includeSet = [NSCharacterSet characterSetWithCharactersInString:includeString];
if ([[string stringByTrimmingCharactersInSet:includeSet] length] > 0)
return NO;
return YES;
}
Note that the empty string does not need to be handled separately anymore.

Is there anyway to limit the number of character in UITextfield to 5?

Iam trying to limit the number of character in a textfield to 5 .So when we trying to enter the 6th character it will not do anything? is that possible .I saw below code but its not working .I have searched this in google got some results like
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 5) ? NO : YES;
}
its not working am still able to add more than 5 character
Please check your delegate connection with UITextfield
Is the delegate being called? If YES, then just try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > 5) {
textField.text = [textField.text substringToIndex:5-1];
return NO;
}
return YES;
}

Resources