Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable the both uialertcontroller button when no input in uitext field of uialertview controller [duplicate]

Look out my below code.I am trying to addthe text field with uialertviewcontroller.Now when there is no input in my both text field i need to disable the both action button.How to do that ??

Please make some possible solutions.Thanks !!

like image 293
mack Avatar asked Aug 12 '16 03:08

mack


1 Answers

Disable the buttons initially and then once you have something in your text field re-enable them.

UIAlertController *alert = [UIAlertController
                                  alertControllerWithTitle:@"Info"
                                  message:@"You are using UIAlertController with Actionsheet and text fields"


preferredStyle:UIAlertControllerStyleAlert];


UIAlertAction* ok = [UIAlertAction
                     actionWithTitle:@"OK"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction * action)
                     {
                         NSLog(@"Resolving UIAlert Action for tapping OK Button");
                         [alert dismissViewControllerAnimated:YES completion:nil];

                     }];
UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             NSLog(@"Resolving UIAlertActionController for tapping cancel button");
                             [alert dismissViewControllerAnimated:YES completion:nil];

                         }];
[ok setEnabled:false];

[cancel setEnabled:false];

[alert addAction:ok];
[alert addAction:cancel];

For adding the selector to the text field and then handling it:

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"iCloud ID";
    textField.textColor = [UIColor blueColor];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [textField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
}];

- (void)textDidChange:(UITextField *)textField {
    if (textField.text.length > 0) {
        // enable the buttons
    }
}

For handling textfield delegates you can go through the following posts:

  • Access input from UIAlertController
  • Check on UIAlertController TextField for enabling the button
like image 140
Harsh Avatar answered Sep 27 '22 22:09

Harsh