Search code examples
iosobjective-cios9uialertcontrolleruiactionsheet

How to disable button action on Action sheet added via UIAlertViewController - (iOS 9/10)


I have made an ActionSheet like this:

enter image description here

Now I want change the color Cancel And Delete to give the feel like they are disabled.

I am able to change the color of all buttons but not individuals.

I have tried using categories but I am keep getting warining that ActionSheet is Depreciated. And the code didn't work also.

I have written this Code:

      - (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled {
for (UIView* view in self.subviews)
{
    if ([view isKindOfClass:[UIButton class]])
    {
        if (buttonIndex == 0) {
            if ([view respondsToSelector:@selector(setEnabled:)])
            {
                UIButton* button = (UIButton*)view;
                button.enabled = enabled;
            }
        }
        buttonIndex--;
    }
}
 }

Is it possible to give disabled look to some of the buttons of Action sheet presented via AlertViewController.

Below is the my code to present Action Sheet:

 UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"Using the alert controller" preferredStyle:UIAlertControllerStyleActionSheet];
 [actionSheet.view setTintColor:[UIColor redColor]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    // Cancel button tappped.


    [self dismissViewControllerAnimated:YES completion:^{
    }];
}]];

[actionSheet addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    // Distructive button tapped.
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}]];

[actionSheet addAction:[UIAlertAction actionWithTitle:@"Group 1" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

    // Distructive button tapped.
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}]];

[actionSheet addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    // OK button tapped.

    [self dismissViewControllerAnimated:YES completion:^{
    }];
}]];


// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];

Solution

  • What you can do is use the setEnabled property of UIAlertAction.

    This will render your button as 'greyed out'.

    I have modified a piece of your code to incorporate this change:

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // OK button tapped.
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }];
    
    [action setEnabled:NO];
    [actionSheet addAction:action];
    

    Ofcourse, you can't click on the button after you disable it.

    Other options would involve using UIAlertActionStyle. There are three:

    UIAlertActionStyleDefault,
    UIAlertActionStyleCancel,
    UIAlertActionStyleDestructive
    

    UIAlertActionStyleDefault will render your button with tintColor.

    UIAlertActionStyleCancel will render your button on the bottom of the sheet.

    UIAlertActionStyleDestructive renders your button with a 'red' color. (Think: Delete button)