2015-11-12 43 views
3

我有一個UIAlertController,我有一堆UIAlertAcion按鈕。現在我需要顯示一個按鈕與其他顏色,而不是相同的顏色。如何更改iOS中的UIAlertAction按鈕顏色?

對於防爆

Button1的

將Button2

將Button3

Button1的和BUTTON3應在藍色

BUTTON2應b e紅色。

可能嗎?怎麼樣?

只是把你的想法 ...

我的代碼:

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Food Menu" message:@"Select the MenuItem" preferredStyle:UIAlertControllerStyleActionSheet]; 
for(int i= 0; i<[menus count];i++){ 

    UIAlertAction *action = [UIAlertAction actionWithTitle:[menu objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){ 
      [actionSheet dismissViewControllerAnimated:YES completion:nil]; 
      //do SomeWork 

      }]; 

      if(i==currentIndex){ 
       //Put button Color Red 
      } 
      else{ 
      //put button color Blue 
      } 
      [actionSheet addAction:action]; 
     } 

     UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){ 
      [actionSheet dismissViewControllerAnimated:YES completion:nil]; 
     }]; 

     [actionSheet addAction:cancel]; 


    [self presentViewController:actionSheet animated:YES completion:nil]; 
} 
+0

你需要編寫自己的視圖達到這個 – zcui93

+0

你的意思是什麼.. U意味着另一個容器視圖...或別的什麼 –

回答

6

做樣改變警報樣式:UIAlertActionStyleDestructive

UIAlertController *alertController = [UIAlertController 
          alertControllerWithTitle:alertTitle 
          message:alertMessage 
          preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *cancelAction = [UIAlertAction 
     actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") 
        style:UIAlertActionStyleCancel 
       handler:^(UIAlertAction *action) 
       { 
        NSLog(@"Cancel action"); 
       }]; 

UIAlertAction *resetAction = [UIAlertAction 
     actionWithTitle:NSLocalizedString(@"Reset", @"Reset action") 
        style:UIAlertActionStyleDestructive 
       handler:^(UIAlertAction *action) 
       { 
        NSLog(@"Reset action"); 
       }]; 

UIAlertAction *okAction = [UIAlertAction 
     actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
        style:UIAlertActionStyleDefault 
       handler:^(UIAlertAction *action) 
       { 
        NSLog(@"OK action"); 
       }]; 

[alertController addAction:cancelAction]; 
[alertController addAction:resetAction]; 
[alertController addAction:okAction]; 
[self presentViewController:alertController animated:YES completion:nil];