Reputation: 1292
Ok, I've run into a small issue here. I'm trying to filter two things in my UITextField. They include limiting the number of characters and filtering the type of characters. I can get each one to work by there self but they both don't work together. It may have something to do with the double returns, idk. Hopefully someone can look at my code and see why they don't work together. I've beat myself up over this. Thanks for the help.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *svo;
svo = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:svo] componentsJoinedByString:@""];
BOOL bT = [string isEqualToString:filtered];
return bT;
if (myTextField.text.length >= MAX_LENGTH && range.length == 0)
{
return NO;
}
else
{
return YES;
}
}
Upvotes: 1
Views: 504
Reputation: 10333
Your first 'return' is the one that always occurs, because you call 'return' unconditionally here.
I think you meant to write this:
return bT || myTextField.text.length < MAX_LENGTH || range.length > 0;
Basically, replace your 'return bT' and the 'if' statement with the above. The basically means that you are returning YES in the following cases:
in all other cases you are returning NO.
Upvotes: 2