I would like to enable the done button on the navbar (in a modal view) when the user writes at least a char in a uitextfield. I tried:
[EDIT]
The solution could be
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
but neither
[self.navigationItem.rightBarButtonItem setEnabled:YES];
or
[doneButton setEnabled:YES]; //doneButton is an IBOutlet tied to my Done UIBarButtonItem in IB
work.
The correct code is;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger length = editingTextField.text.length - range.length + string.length;
if (length > 0) {
yourButton.enabled = YES;
} else {
yourButton.enabled = NO;
}
return YES;
}
Edit: As correctly pointed out by MasterBeta before and David Lari later, the event should respond to Editing Changed. I'm updating the answer with David Lari's solution as this was the one marked as correct.
- (IBAction)editingChanged:(UITextField *)textField
{
//if text field is empty, disable the button
_myButton.enabled = textField.text.length > 0;
}