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

3

我有一个UIAlertController和一堆UIAlertAcion按钮。现在,除了相同的颜色之外,我需要显示一个带有其他颜色的按钮。

例如:

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

你需要编写自己的视图来实现这个功能。 - zc246
你的意思是什么?是指另一个容器视图吗?还是其他什么? - SUDHAKAR RAYAPUDI
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接