如何禁用通过UIAlertViewController添加动作片按钮操作 - (iOS版9/10)
问题描述:
我做了一个ActionSheet这样的:如何禁用通过UIAlertViewController添加动作片按钮操作 - (iOS版9/10)
现在我想改变颜色Cancel
而Delete
给予感觉他们是disabled
。
我能够改变所有按钮的颜色,但不是个人。
我试过使用类别,但我一直在得到warining
ActionSheet
是Depreciated
。代码也没有工作。
我写了这个代码:
- (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];
答
你可以做的是使用的UIAlertAction
的setEnabled
财产。
这将使您的按钮呈现“变灰”。
我已经修改了一段代码的将这一变化:
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按钮)
答
获取操作数组并更改所需操作的状态。 通过这种方式,即使在添加动作后,您也可以更改状态。
以下是示例代码。
NSArray* actionlist = actionSheet.actions;
UIAlertAction * action2 = [actionlist objectAtIndex:1];
action2.enabled = false;
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
如果你想*禁用*的行为,为什么你不有条件地忽略它们? – vadian
@vadian我必须向用户展示这些是可用的选项。但是你不能选择直到你满足条件。 (如IAP) – Dalvik