问题描述:

I found this code from another stack overflow post

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSUInteger upperRightLength = [upperRight.text length] + [string length] - range.length;

return (upperRightLength > 1) ? NO : YES;

}

I need to run this same code (which works great) on more than one textField..

I tried this but it did not seem to run correctly, and did not have the same effect!

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSUInteger upperRightLength = [upperRight.text length] + [string length] - range.length;

return (upperRightLength > 1) ? NO : YES;

NSUInteger upperLeftLength = [upperLeft.text length] + [string length] - range.length;

return (upperLeftLength > 1) ? NO : YES;

}

I don't know what else to do! Thank you so much for your help!!

EDIT:

I have now tried another code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSUInteger upperRightLength = [upperRight.text length] + [string length] - range.length;

NSUInteger upperLeftLength = [upperLeft.text length] + [string length] - range.length;

return (upperLeftLength > 1 && upperRightLength > 1 ) ? NO : YES;

}

Now the same thing seems to happen. Which ever text field I click on first (lets say upperLeft) I am still able to type as many characters as I want. But then when I click on the other textfield (upperRight) I am only able to type 1 character. I have tried starting with the (upperRight) first and I see the same results... where the (upperRight) will allow me to type as many as I want, and the (upperLeft) will only allow me to type 1 character! This is so weird!! Thank you for your help!!

网友答案:

I think you are missing the point of the delegate method. This method gives you the textField that is being edited so you can use the reference to textField given to you and not use an ivar

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
  NSUInteger length = [textField.text length] + [string length] - range.length;
  return !(length > 1);
}

update

There are many different ways to handle different lengths for different fields. The simplest would be something like:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
  NSInteger maxLength = 1;

  if (textField == self.myOtherTextField) {
    maxLength = 2;
  }

  NSUInteger length = [textField.text length] + [string length] - range.length;
  return !(length > maxLength);
}
网友答案:

You'll never calculate upperLeftLength because you return just before. So, what you need to do is calculate upperLeftLength and upperRightLength, then determine if it returns YES or NO. Put a breakpoint on the line upperLeftLength, you see that your breakpoint will never be hit.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger upperRightLength = [upperRight.text length] + [string length] - range.length;
    NSUInteger upperLeftLength = [upperLeft.text length] + [string length] - range.length;
    return (upperLeftLength > 1 &&  upperRightLength > 1 ) ? NO : YES;
}

would work.

网友答案:

A better way would be to use the didChange method like the UITextViewDelegate method, but as we know the UITextFieldDelegate does not have a didChange method. You can manually add behaviour. You can use the shouldChangeCharactersInRange: method, but personally I would advise not to override methods unless you absolutely have to.

You can add behaviour using:

[yourTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

And in the target method:

- (void)textFieldDidChange:(UITextField*)textField{

    //handle code
}

The advantage here is, you do not have to wait for the user to type another character before knowing your code should take effect. As soon as they reach the limit, your code will take effect.

If your textFields are global, you may access them here directly with textField == upperRight, or if they are defined in blocks, just set their tags and check with textField.tag == <tagValueOfTextField>.

相关阅读:
Top