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

标签 ios objective-c ios9 uialertcontroller uiactionsheet

我制作了一个这样的ActionSheet:

enter image description here

现在,我想更改颜色CancelDelete,以使其具有disabled的感觉。

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

我尝试使用类别,但是我一直在获取wariningActionSheetDepreciated。而且代码也不起作用。

我写了这段代码:

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

是否有可能使通过Action sheet呈现的AlertViewController的某些按钮显示为禁用状态。

以下是我提供操作表的代码:
 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];

最佳答案

您可以做的是使用setEnabledUIAlertAction属性。

这将使您的按钮显示为“灰色”。

我已经修改了一部分代码以合并此更改:

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将在页面底部呈现您的按钮。
UIAlertActionStyleDestructive用“红色”颜色呈现按钮。 (认为​​:“删除”按钮)

关于ios - 如何禁用通过UIAlertViewController添加的操作表上的按钮操作-(iOS 9/10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675500/

相关文章:

ios - Swift 自定义 NSMutableArray 排序

ios - Swift @IBDesignable/@IBInspectable UIView 样式

ios - 使用添加为 subview 的按钮自动调整 TableView 单元格

objective-c - stringByTrimmingCharactersInSet : is not removing characters in the middle of the string

objective-c - NSURLSessionTask 的 AFNetworking 1.x 到 3.x 迁移?

html - 无法加载资源,插件在 iOS 上处理加载

ios - 如何在 iOS 中创建分层菜单?

iphone - NSHTTPCookieStorage 是否跨应用程序持久存在?

ios - iOS 9 中的 UITapGestureRecognizer 问题

ios - iPv6 支持可达性等级