6

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 !!

mack
  • 603
  • 2
  • 9
  • 22
  • Please see http://stackoverflow.com/questions/24474762/check-on-uialertcontroller-textfield-for-enabling-the-button I'd mark it a duplicate but it's in Swift. – rmaddy Aug 12 '16 at 04:17
  • 1
    try this , it will help your http://stackoverflow.com/questions/32298679/enable-uialertaction-of-uialertcontroller-only-after-input – caldera.sac Aug 12 '16 at 04:20

1 Answers1

15

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:

shim
  • 9,289
  • 12
  • 69
  • 108
Harsh
  • 2,852
  • 1
  • 13
  • 27
  • I din get you the solution.What happen if my two text field empty.Then how i will set my both button as disable.And if my both text field is fill then how can i enable my both button – mack Aug 12 '16 at 04:11
  • As per my above question code.How can i call my text field in if statement to set enable my button – mack Aug 12 '16 at 04:16
  • Make two class level variables for your two actions. Set these actions initially as disabled like i have done in the example above. Now set the delegate of the textfields which you have added in the action as self and implement the `UITextFieldDelegate`. Once both of your textfields have content, enable the actions using the classvariables you created. – Harsh Aug 12 '16 at 04:16
  • Can you please give some example code for the uitextfield delegate to enable my button if uitext filed is fill – mack Aug 12 '16 at 04:17
  • http://stackoverflow.com/questions/24474762/check-on-uialertcontroller-textfield-for-enabling-the-button/24476797 – Harsh Aug 12 '16 at 04:18
  • Please check out this post and if you still don't understand, let me know – Harsh Aug 12 '16 at 04:18
  • http://stackoverflow.com/questions/24172593/access-input-from-uialertcontroller – Harsh Aug 12 '16 at 04:19
  • i want to enable the button if the text field is fill.Then you gave the suggestion that i need to do uidelegate method for this right ??.Thats what i have asked some example code for uitext field delegate method.I am using objective c.I am beginner.So i dont know swift – mack Aug 12 '16 at 04:20
  • Updated the answer – Harsh Aug 12 '16 at 04:29
  • Then if say i have two text field Like textfield1,textfield 2.Then should i need to do two selector method ?? – mack Aug 12 '16 at 04:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120756/discussion-between-harsh-and-mack). – Harsh Aug 12 '16 at 04:37