How to disable the alertview button in iPhone?

I have a warning with two OK and Cancel buttons and a text box. Now I want to disable the OK button until the user enters text in the text box. How can i do this? thanks in advance

+7
source share
5 answers

You can create two buttons for OK and Cancel. Then add these two as submenus in the UIAlertView. Now, having checked the text (text length) in the text field, you can perform actions to enable and disable.

+1
source

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
 [<#your alert#> addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.delegate = self; textField.tag = 0; //set a tag to 0 though better to use a #define }]; 

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{ //inside the notification is the object property which is the textField //we cast the object to a UITextField* if([[(UITextField*)notification.object text] length] == 0){ //The UIAlertController has actions which are its buttons. //You can get all the actions "buttons" from the `actions` array //we have just one so its at index 0 [<#your alert#>.actions[0] setEnabled:NO]; } else{ [<#your alert#>.actions[0] setEnabled:YES]; } } 

Remember to remove your observer when done.

+37
source

I wanted to extend Ryan Forsyth's answer by adding this. If you add the default UIAlertView, you can get an out of range exception if you try to access a text field because it is not there, so check your presentation style first.

 -(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView { if(alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput || alertView.alertViewStyle == UIAlertViewStylePlainTextInput || alertView.alertViewStyle == UIAlertViewStyleSecureTextInput) { NSString* text = [[alertView textFieldAtIndex:0] text]; return ([text length] > 0); } else if (alertView.alertViewStyle == UIAlertViewStyleDefault) return true; else return false; } 
+4
source

Without knowing the context of your application, the following may not apply - but have you read the iOS Human Interface Guides ? It sounds as if you might be better off finding an alternative to UIAlertView if it is often displayed to the user.

0
source

Not relevant to your question, but do not change the default UIAlertView if you do not want to reject your application. If I'm not mistaken, you add text fields to the alertview, right? Like a view of the entrance. You must create your own look.

So, regarding your question, create your own view, set the buttons off and delegate UITextFields. When

 - (void)textFieldDidBeginEditing:(UITextField *)textField; 

activate these buttons.

0
source

All Articles