如何在通过 UIAlertViewController 添加的 Action sheet 上禁用按钮操作 - (iOS 9/10)

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

我做了一个这样的操作Sheet:

现在我想更改 CancelDelete 的颜色,给人以 disabled 的感觉。

我可以更改所有按钮的颜色,但不能更改个别按钮的颜色。

我尝试过使用类别,但我一直 warining ActionSheetDepreciated。而且代码也不起作用。

我写了这段代码:

      - (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--;
    }
}
 }

是否可以为通过 AlertViewController 显示的 Action sheet 的某些按钮提供禁用外观。

下面是我的代码来呈现操作 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];

你可以做的是使用 UIAlertActionsetEnabled 属性。

这会将您的按钮呈现为 'greyed out'。

我修改了您的一段代码以合并此更改:

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

[action setEnabled:NO];
[actionSheet addAction:action];

当然,禁用后不能点击按钮

其他选项将涉及使用 UIAlertActionStyle。 一共有三个:

UIAlertActionStyleDefault,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive

UIAlertActionStyleDefault 将使用 tintColor 呈现您的按钮。

UIAlertActionStyleCancel 会将您的按钮呈现在 sheet 的底部。

UIAlertActionStyleDestructive 用 'red' 颜色呈现您的按钮。 (思考:删除按钮)

获取动作数组并更改所需动作的状态。 这样你甚至可以在添加动作后改变状态。

这是示例代码。

NSArray* actionlist = actionSheet.actions;
UIAlertAction * action2 = [actionlist objectAtIndex:1];    
action2.enabled = false;

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