2017-01-16 63 views
0

我做了一個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

回答

3

你可以做的是使用的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

1

獲取操作數組並更改所需操作的狀態。 通過這種方式,即使在添加動作後,您也可以更改狀態。

以下是示例代碼。

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

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