如何禁用通过UIAlertViewController添加动作片按钮操作 - (iOS版9/10)

问题描述:

我做了一个ActionSheet这样的:如何禁用通过UIAlertViewController添加动作片按钮操作 - (iOS版9/10)

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]; 
+0

如果你想*禁用*的行为,为什么你不有条件地忽略它们? – vadian

+0

@vadian我必须向用户展示这些是可用的选项。但是你不能选择直到你满足条件。 (如IAP) – Dalvik

你可以做的是使用的UIAlertActionsetEnabled财产。

这将使您的按钮呈现“变灰”。

我已经修改了一段代码的将这一变化:

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

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

Ofcourse,你不能按钮禁用后,单击。

其他选项将涉及使用UIAlertActionStyle。 有三种:

UIAlertActionStyleDefault, 
UIAlertActionStyleCancel, 
UIAlertActionStyleDestructive 

UIAlertActionStyleDefault会使您的按钮,tintColor

UIAlertActionStyleCancel将呈现在工作表底部的按钮。

UIAlertActionStyleDestructive呈现您的按钮'红'的颜色。 (Think:Delete按钮)

+0

这是快速工作吗?正如我尝试actionSheetController.isEnabled = false。但提供错误“值的类型'UIAlertController'没有成员'isEnabled' – Amanpreet

+0

是的,这也应该在Swift中工作请注意我设置'setEnabled'在'UIAlertAction'上,而不是在'UIAlertController'上 – Ramon

+1

Yup!我注意到了,谢谢! – Amanpreet

获取操作数组并更改所需操作的状态。 通过这种方式,即使在添加动作后,您也可以更改状态。

以下是示例代码。

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

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