1

I know this may be perceived as a duplicate but I promise you it is not.

Everybody has given an answer similar to this:

if ([_confirm.text isEqualToString: _password.text] || resultantLength2 < 3) {
            textField.textColor = [UIColor redColor];
            [_next setEnabled:NO];

        }else{
            [_next setEnabled:YES];
        }
}

So why does this not work?

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

if (textField == _confirm || textField == _password) {
        int resultantLength2 = textField.text.length + string.length - range.length;

        NSLog(@"Length After Additions And Subtractions Will Be: %i letters", resultantLength2);

        if ([_confirm.text isEqualToString: _password.text] || resultantLength2 < 3) {
            textField.textColor = [UIColor redColor];
            [_next setEnabled:NO];

        }else{
            [_next setEnabled:YES];
        }
    }
    return YES;
}

Where I have a next button that gets dissabled

maxisme
  • 3,974
  • 9
  • 47
  • 97

2 Answers2

4

What we actually want to do is compare the strings in the text fields AFTER the text has changed, but currently, we're using a method that is fired BEFORE the text changes and asks whether or not the change is okay.

We can do some tricks in this method to get it work right, but why not just check the values after they've changed?

In the view controller's viewDidLoad, add these two lines:

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

Now add the textFieldDidChange: method to compare the values in the textfield.

- (void)textFieldDidChange:(UITextField *)textField {
    if (![_confirm.text isEqualToString: _password.text]) {
        textField.textColor = [UIColor redColor];
        [_next setEnabled:NO];
    } else {
        [_next setEnabled:YES];
    }
}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

Your problem is that you are in shouldChangeCharactersInRange, which is called before the text field is updated, but you are comparing the current value of _confirm.text

Also, your string comparison logic seems to be the wrong way around - you want NEXT enabled if the password is the same as the confirm I would think

I would try this -

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



if (textField == _confirm) {

        NSMutableString *newValue=[NSMutableString stringWithString:textField.text];
        [newValue replaceCharactersInRange:range withString:string];

        int resultantLength2 = newValue.length;

        NSLog(@"Length After Additions And Subtractions Will Be: %i letters", resultantLength2);

        if (([newValue isEqualToString: _password.text] == NO) || resultantLength2 < 3) {
            textField.textColor = [UIColor redColor];
            [_next setEnabled:NO];

        }else{
            textField.textColor = SOMETHING; // should reset text colour
            [_next setEnabled:YES];
        }
    }
    return YES;
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Thank you so much This works apart from the text color?! – maxisme Apr 08 '14 at 22:21
  • Which text colour? The red or the SOMETHING? You need to replace SOMETHING with your normal text colour, possibly [UIColor blackColor], but I wasn't sure what colour you are using – Paulw11 Apr 08 '14 at 22:44
  • http://stackoverflow.com/questions/18991149/uitextfield-secureentrytext-color-changes it is to do with this but I don't quite understand – maxisme Apr 08 '14 at 22:44
  • What exactly are you seeing? I just tried modifying textColor in a `shouldChangeCharactersInRange:` method and it worked correctly. This is on iOS 7.1 – Paulw11 Apr 08 '14 at 22:59
  • I have changed the soomething but it is just staying `[UIColor blackColor];` – maxisme Apr 08 '14 at 23:03
  • Is your text field a subclass of UITextField or just a straight UITextField? – Paulw11 Apr 08 '14 at 23:41
  • a straight UITextField – maxisme Apr 08 '14 at 23:58
  • How are you creating and initializing the field? With IB? As I said I tried changing the color as I typed and it worked correctly. Are you sure you aren't setting the color anywhere else ? – Paulw11 Apr 09 '14 at 02:08
  • Also, as per @nhgrif's comment did you want to change the color of the confirm field or the password field or both? – Paulw11 Apr 09 '14 at 02:11
  • just the confirm field! I dont understand why it is not working! It works if I uncheck the secure radio button... – maxisme Apr 09 '14 at 10:17
  • Which version of iOS? – Paulw11 Apr 09 '14 at 12:09
  • It is strange. I suggest you create a quick app that just implements a couple of text fields on a view and verify that it works there. If it does then it must be something in your current app. – Paulw11 Apr 09 '14 at 20:31