I have searched the site for my answer, however I think it is also how I got myself into this problem.
I'm trying to limit my 1 and only text field to 10 characters and only numbers.
Below is code mostly off other questions on this site. What I am trying to do is mash this code together for my limits
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
NSString *newString = [myTextField.text stringByReplacingCharactersInRange:range withString:filtered];
return !([newString length] > 10);
}
There are no problems with my build until I click my textfield... but I get a green arrow warning stating " thread stopped at breakpoint 1 " I've tried a few different things now, but now feel I am stumped, I'm hoping this is something real easy for someone here...
they will work individually (one or the other) with the code below, but I can't seem to get them working together.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [myTextField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] > 10);
}
This will limit my input to 10 and ...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
This also restricts my input to numbers only with no problems. provided I defined LEGAL as only numbers...
I just can't seem to get them to work together... Can anyone help me with this one ???
There are many ways to do this, but I think this might be the most understandable:
The first section essentially check a non-empty string for characters that are not between '0' and '9' if it finds one anywhere in the new string it rejects the edit. The second determines what the length of the text will be if the editing is completed and compares to 10 if it's to high it rejects the edit. If the function passes all those tests then the edit is allowed.
You have apparently set a breakpoint in your
textField:shouldChangeCharactersInRange:replacementString:
method. If your not familiar with breakpoints they look like this: (The blue arrow on the row numbers column) And you can toggle whether or not they are enabled with the button pictured on the right:Breakpoints are very handy, and I suggest you learn to use them. But for now simply click on the breakpoint and drag it from the column, like an icon from the dock, or toggle that option off.