Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable a UIButton until there is text in the UITextField

How do i disable a UIButton until there is text in the UITextField. The code i have doesn't seem to work.

    if ([nameTextField.text isEqualToString: @""]) {
            [myButton setEnabled:NO];
}
like image 688
Sohan Avatar asked Apr 21 '11 13:04

Sohan


1 Answers

Here's the code from kingthong's blog post, which worked for me:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.saveButton setEnabled:NO];

    [[self myTextField] becomeFirstResponder];
    [myTextField addTarget:self 
                    action:@selector(textFieldDidChange)
          forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldDidChange
{
    if ([self.myTextField.text isEqualToString:@""]) {
        [self.saveButton setEnabled:NO];
    }
    else {
        [self.saveButton setEnabled:YES];
    }
}
like image 72
nevan king Avatar answered Sep 19 '22 11:09

nevan king