0

I'm using:

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

I want this to only apply to the phone number field, whose tag is 1, so in this function I put

if(textField.tag != 1) { return NO; }

After I did this, I cannot even input anything into the other text fields. How can I fix this?

Michael McKenna
  • 811
  • 1
  • 11
  • 24
  • What exactly are you trying to achieve? – tilo Nov 12 '15 at 21:00
  • I'm formatting the phone number field using what was posted here:http://stackoverflow.com/questions/6052966/phone-number-formatting and I don't want it to affect the password field – Michael McKenna Nov 12 '15 at 21:01
  • Make sure that your other textfields have different tag – arturdev Nov 12 '15 at 21:01
  • The other field does have a different tag - the password field's tag is 2. – Michael McKenna Nov 12 '15 at 21:02
  • Instead of using that if to return NO, use `if (textField.tag == 1) {...)` and use the ... to format the phone number in that text field. – keithbhunter Nov 12 '15 at 21:04
  • It's better to compare against a reference to the interesting textField, rather than using the tag. Something like `if (textField == self.phoneField) ...`. – Avi Nov 13 '15 at 08:09

1 Answers1

2

If you want to do the formatting just for a specific text field (with tag=1):

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: 
   (NSRange)range replacementString:(NSString *)string{
    if (textField.tag == 1) {
        // formatting here
    } else {
        return YES;
    }
} 

If you return NO for other text fields, you explicitly state that changes are not allowed.

tilo
  • 14,009
  • 6
  • 68
  • 85