开发者

iPhone UITextField filtering doesn't work properly

开发者 https://www.devze.com 2022-12-15 19:21 出处:网络
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 wo

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;
}
}


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:

  • bT is YES
  • or the text length in the text field is less than MAX_LENGTH
  • or the range length is positive

in all other cases you are returning NO.

0

精彩评论

暂无评论...
验证码 换一张
取 消