Post this only to update the answer, since ios 5:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { UITextField *textField = [alertView textFieldAtIndex:0]; if ([textField.text length] == 0) { return NO; } return YES; }
UPDATE : iOS 8 Since Apple rejected UIAlertView in favor of UIAlertController. You no longer need to delegate the alertViewShouldEnableFirstOtherButton: call alertViewShouldEnableFirstOtherButton:
So, instead, you should set the enabled property using the UITextFieldTextDidChangeNotification button UITextFieldTextDidChangeNotification Add text to the alert using
- (void) addTextFieldWithConfigurationHandler: (void (^) (UITextField * textField)) configurationHandler
[<
Then we implement the delegate method
- (void) textFieldDidBeginEditing: (UITextField *) textField
- (void)textFieldDidBeginEditing:(UITextField *)textField{ //in here we want to listen for the "UITextFieldTextDidChangeNotification" [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldHasText:) name:UITextFieldTextDidChangeNotification object:textField]; }
When the text in the text field changes, it calls the "textFieldHasText:" call and passes NSNotification *
-(void)textFieldHasText:(NSNotification*)notification{
Remember to remove your observer when done.
RyanTCB
source share