0

I have in my TableViewCell 2 TextFields.

For the 2nd TextField i need a confirm to enable a button.

I´m using this code to determine if the confirm button should be enabled, but it doesn't work:

-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    textField = self.installDeviceCell.passWordTextFieldOutlet;
    [textField addTarget:selfaction:@selector(checkTextField:)forControlEvents:UIControlEventEditingChanged];
    return YES;
}

-(void)checkTextField:(id)sender
{
    UITextField* textField = (UITextField*)sender;
    if ([textField.text length] < 7) {
        self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = NO;
    }else if ([textField.text length] > 7){
        self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = YES;
    }else{
        self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = YES;
    }
}
alexgophermix
  • 4,189
  • 5
  • 32
  • 59
Andy
  • 1

1 Answers1

0

Have you assigned your UITextFieldDelegate for your UITextField when it is being instatiated? If don't assign the it the delegate methods for the UITextField will not be called.

Also make sure that in the header file of the delegate you specify the delegate of the interface e.g.

@interface MyDelegate <UITextFieldDelegate>
// ...
@end

Finally you're doing your text calculations a little oddly. You can do all your computations witin shouldChangeCharactersInRange. You also don't need to add the target or overwrite the textField property. Simply check if the replacementString is greater than 7 characters. Your final code might look something like this:

-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string length] < 7) {
        self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = NO;
    } else {
        self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = YES;
    }
    return YES;
}

See this great answer on delegation for more info: https://stackoverflow.com/a/626946/740474

Community
  • 1
  • 1
alexgophermix
  • 4,189
  • 5
  • 32
  • 59
  • Yes, UITextFieldDelegate is instantiated – Andy Nov 26 '13 at 21:12
  • In that case it's probably an issue with how you do your computation. See my edited answer for more details. Also try stepping through the debugger when a `textField:shouldChangeCharactersInRange:replacementString:delegate` method is fired and making sure it's behaving as expected (or being called at all) – alexgophermix Nov 26 '13 at 21:21