如何更改iOS中的UIAlertAction按钮颜色?

时间:2015-11-12 13:54:42

标签: ios objective-c uialertcontroller uialertaction

我有一个UIAlertController,我有一堆UIAlertAcion按钮。现在我需要显示一个带有其他颜色而不是相同颜色的按钮。

对于Ex

  

Button1的

     

将Button2

     

将Button3

     

Button1和button3应为蓝色

     

     

button2应为红色。

有可能吗?怎么样?

只是扔掉你的想法 ...

我的代码:

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

1 个答案:

答案 0 :(得分: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];