Search code examples
iosobjective-cios5ios6uitextview

How to keep Done button disabled until user types something into the UITextView?


As the title states, if the user types into the textview, the Done button becomes enabled. But if they erase everything, it becomes disabled again. For a UITextField I just added a target for when it changed and checked then, but I see no such method for UITextView.

I tried to implement the delegate as follows, but it doesn't work either:

- (void)textViewDidChange:(UITextView *)textView {
    if ([textView hasText]) {
        self.doneButton.enabled = YES;
    }
    else {
        self.doneButton.enabled = NO;
    }
}

Note that on viewDidLoad I set the textview to being first responder.


Solution

  • You can use UITextViewDelegate's – textViewDidChange: method, as you already have. You also must first set the textView's delegate property to the instance of a class that is conforming to the delegate protocol, and implementing this method.

    Usually you set textView.delegate = self; and conform to the protocol in the .h file by adding <UITextViewDelegate> after the superclass name.